Skip to content

Instantly share code, notes, and snippets.

@HenestrosaDev
Last active April 17, 2022 11:11
Show Gist options
  • Save HenestrosaDev/93e3440aacb34cdbf4fd11091be026fa to your computer and use it in GitHub Desktop.
Save HenestrosaDev/93e3440aacb34cdbf4fd11091be026fa to your computer and use it in GitHub Desktop.
Linux commands

LINUX COMMANDS

Some things that you need to know beforehand

  • The explanation of the programs and commands is based on my understanding of them. However, I have contrasted them using the program man and the flag --help as a reference in order to give the simplest yet most accurate explanation to each one. Every external reference has been indicated in its respective part.
  • You might be missing some programs/flags. I am listing the ones that I have used so far, so I might be overlooking some useful utilities. Feel free to improve the list with the addition of more, with their respective explanation.
  • If you have any trouble finding out what a program does, simply use man [PROGRAM], info [PROGRAM] or whatis [PROGRAM] to retrieve more information about it.
  • Some commands are written like this one: rm [-r|--recursive]. That means that the commands rm -r and rm --recursive are both possible inputs. If the description of a command like this does not specify the difference between them, it is because there is none.
  • In brackets and in uppercase (for example, [FILE]), it is specfied what type of argument is needed for a certain command. For example, a real use of cat [FILE] would be cat foo.txt.
  • If you want to exit the execution of a command and typing and executing exit does not work, press Ctrl + C to interrrupt it.
  • Bear in mind that some flags can be combined, depending on their function. For example, ls -a and ls -l can be merged as ls -a -l or, even better, as ls -al.
  • You will have to be root or superuser in order to execute some of the commands listed. Check whether you are the root user by executing whoami or, if you are not, make sure to add sudo at the beginning of the given command. You can also execute sudo su - for log in as the root user.
  • On the left side of the CLI (Command Line Interface), you will notice something like this: jc@jc:~$.

    jc - Current user jc - Group to which the user belongs ~ - Current path. ~ is the user's home directory. / is the very top of the filesystem. $ - States that the user is logged in as a normal user. It could also be #, which would mean that the user is the system administrator (root).

  • Whenever you encounter a in a command, bear in mind that its meaning is that multiple arguments can be passed to it. For example, cat [FILE] … means that more files can be passed to it, like this: cat foo.txt bar.txt.

Concepts:


DOCUMENTATION

  • whatis [PROGRAM] for printing a single-line description of the given program.

  • man [PROGRAM] for retrieving a detailed description of what the program do along with its flags.

  • info [PROGRAM] for retrieving the info documents of the program. It is good for contrasting some information found with the man program.

  • [PROGRAM] --help for printing in the console the flags that the program accepts.

  • For instance, open the terminal and run man rm or info rm. man will tell you what the rm program does, how it works, and the available flags/options. For rm, the -r flag means:

    -r, -R, --recursive
                remove directories and their contents recursively
    

    You can see that there are multiple ways to set this flag as well. You could run rm -r, rm -R, or even rm --recursive, all will do the same thing. However, each program has its own flags and, for example, -R does not do the same with the command rm as it does with the command ls, so make sure to check the documentation for each program when working with them. Reference.

SYSTEM

  • date for showing the current date and time.
  • ./[EXECUTABLE FILE] for running an executable. It is not a command by itself but it lets your shell run an executable file with any interpreter installed in oyr system directly from the terminal, without the need of specifying said interpreter. We can run a Bash file as well as a Python script indistinctly. E.g. ./python_script.py. Remember that the file needs to have the x permission (explained below).
  • clear for bringing the command line on top of the computer terminal.
  • exit for exiting the execution of a program (not all though) and/or exiting the shell currently running.
  • echo "[TEXT]" for displaying a defined text in the terminal. Its primary usage is to print environmental variables inside those messages such as echo "Hey $USER"
  • history for displaying an enumerated list with the commands used in the past.
  • !! for repeating the previously executed command.
  • uptimefor showing how long the system has been running.
  • which [PROGRAM] for retrieving the directory in which a program is installed.

uname

  • uname for printing the name of the kernel. Stands for Unix name.
  • uname [-a|--all] for printing all available information. E.g. Linux jc 5.4.0-107-generic #121-Ubuntu SMP Thu Mar 24 16:07:22 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux

    Linux - Kernel name. jc - Hostname. 5.4.0-107-generic - Kernel release. #121-Ubuntu SMP Thu Mar 24 16:07:22 UTC 2022 - Kernel version. aarch64 - Machine hardware name. GNU/Linux - Operating system name.

  • uname [-s|--kernel-name] for printing the kernel name. Output example: Linux. Same as executing uname.
  • uname [-n|--nodename] for printing the network node hostname. Output example: jc.
  • uname [-r|--kernel-release] for printing the kernel release. Output example: 5.4.0-107-generic.
  • uname [-v|--kernel-version] for printing the kernel version. Output example: #121-Ubuntu SMP Thu Mar 24 16:07:22 UTC 2022.
  • uname [-m|--machine] for printing the machine hardware name. Output example: aarch64.
  • uname [-p|--processor] for printing the processor type. Output example: aarch64.
  • uname [-i|--hardware-platform] for printing the hardware platform. Output example: aarch64.
  • uname [-o|--operating-system] for printing the operating system. Output example: GNU/Linux.

neofetch

  • neofetch is a command-line system information tool written displays information about your operating system, software and hardware in an aesthetic and visually pleasing way. It has to be installed via apt, like this: sudo apt install neofetch.

    Example
                  .-/+oossssoo+/-.               jc@jc 
              -:+ssssssssssssssssss+:-           ----- 
            -+ssssssssssssssssssyyssss+-         OS: Ubuntu 20.04.4 LTS aarch64 
          .ossssssssssssssssssdMMMNysssso.       Host: QEMU Virtual Machine virt-6.2 
         /ssssssssssshdmmNNmmyNMMMMhssssss/      Kernel: 5.4.0-107-generic 
        +ssssssssshmydMMMMMMMNddddyssssssss+     Uptime: 1 day, 4 hours, 58 mins 
       /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Packages: 1659 (dpkg), 4 (snap) 
      .ssssssssdMMMNhsssssssssshNMMMdssssssss.   Shell: bash 5.0.17 
      +sssshhhyNMMNyssssssssssssyNMMMysssssss+   Resolution: 1440x837 
      ossyNMMMNyMMhsssssssssssssshmmmhssssssso   DE: GNOME 
      ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM: Mutter 
      +sssshhhyNMMNyssssssssssssyNMMMysssssss+   WM Theme: Adwaita 
      .ssssssssdMMMNhsssssssssshNMMMdssssssss.   Theme: Yaru [GTK2/3] 
       /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/    Icons: Yaru [GTK2/3] 
        +sssssssssdmydMMMMMMMMddddyssssssss+     Terminal: gnome-terminal 
         /ssssssssssshdmNNNNmyNMMMMhssssss/      CPU: (4) 
          .ossssssssssssssssssdMMMNysssso.       GPU: 00:01.0 Red Hat, Inc. Virtio GP 
            -+sssssssssssssssssyyyssss+-         Memory: 851MiB / 3916MiB 
              -:+ssssssssssssssssss+:-
                  .-/+oossssoo+/-.           
      

shutdown

  • shutdown now for shutting down the system now.
  • shutdown [-r|--reboot] [TIME] for rebooting the system at a specific time.
  • shutdown [-p|--poweroff] [TIME] for powering off the system at a specific time.
  • shutdown [HH:MM] for shutting down the system at a specific time.
  • shutdown -c for canceling a scheduled shutdown.

PROCESSES AND SERVICES

  • service [NAME] [ACTION] for [status|stop|start|restart] a service. Each possible input will do different things, as one can expect based on their name. E.g. sudo proftpd status .
  • htop for opening an interactive process viewer that lets you manage your machine's resources directly from the terminal. In most cases, it isn’t installed d by default, so make sure to read more about it on its download page.
  • ps for displaying your currently active processes. Stands for process status.
    PID TTY          TIME CMD
    7480 pts/1    00:00:00 bash 
    7486 pts/1    00:00:00 ps
    
  • kill [PID] for killing a process with the given Process Identifier (PID).

USERS

  • sudo [COMMAND] for executing a command as superuser. Stands for superuser do.
  • sudo su - for logging in as a root user. su stands for switch user.
  • whoami for showing the name of the current user logged-in.

passwd

  • passwd for changing the password of the current user logged-in.
  • passwd [USER] for changing the password of the given user.
  • passwd -g for changing the password of the current group to which the current user logged-in belongs.
  • passwd -g [GROUP] for changing the password of the given group.
  • passwd -e [USER] for forcing users to change their password the next time they log in.
  • passwd -l [USER] for locking the user's password
  • passwd -u [USER] for unlocking the user's password.

PACKAGE MANAGER

Notice that you will have to execute all these commands with sudo or as root user.

  • apt install [PACKAGE] for installing a package.
  • apt reinstall [PACKAGE] for reinstalling a package.
  • apt remove [PACKAGE] for removing a package.
  • apt purge [PACKAGE] for removing a package along with its configuration.
  • apt upgrade [PACKAGE] for upgrading a certain package (if available for upgrading).
  • apt show [PACKAGE] for showing all details of a package, such as the author, the version and the description.
  • apt update for refreshing repository index.
  • apt upgrade for upgrading all packages available for upgrading (if any).
  • apt full-upgrade for upgrading all packages and installing, removing and upgrading packages as apt deems necessary.
  • apt autoremove for removing unwanted packages.
  • apt list --upgradable for listing all packages available for upgrading.
  • apt list --installed for listing all installed packages.
  • apt edit-sources for changing the source of the packages.
  • apt-get and apt-cache have been replaced by apt but you might see them listed in some old tutorials. As you might think due to the statements at the beginning, it is prefered to use apt over apt-get and apt-cache due to its abstraction and simplification. Here is an interesting article explaining in detail the differences between apt and apt-get/apt-cache.

FILE

List

  • Beyond knowing the commands, which is important, it is vital to know the information displayed by some of these commands. For example, if we execute ls -l, something like the line below will be displayed, depending on your files, computer, permissions… The following explanation has been taken from here with slight changes made by me:

    -rwxrw-r--    10    root   root 2048    Jan 13 07:11 afile.exe
    ?UUUGGGOOOS   00  UUUUUU GGGGGG ####    MON DD XX:XX FILENAME
    ^ ^  ^  ^ ^    ^      ^      ^    ^      ^            └ Filename
    | |  |  | |    |      |      |    |      └-------------- Time of last modification
    | |  |  | |    |      |      |    └--------------------- File size. Some versions and/or command line flags will list the number of blocks used instead of the number of bytes. A filesystem with a block size of 1024 bytes will list all sizes up to 1024 bytes as 1, meaning 1 block is used; from 1025 to 2048 as 2, using 2 blocks; and so on. However, listing block sizes by default (without explicitly using a command line option) is rare on most modern Unix machines
    | |  |  | |    |      |      └-------------------------- Group Name (for example, Users, Administrators, etc)
    | |  |  | |    |      └--------------------------------- Owner's name
    | |  |  | |    └---------------------------------------- Number of [hard links](https://www.cbtnuggets.com/blog/certifications/open-source/linux-hard-links-versus-soft-links-explained#:~:text=In%20Linux%20systems%2C%20the%20data%20structure%20that%20does%20the%20actual%20storing%20of%20information%20is%20called%20an%20Inode.%20A%20hard%20link%20is%20a%20file%20all%20its%20own%2C%20and%20the%20file%20references%20or%20points%20to%20the%20exact%20spot%20on%20a%20hard%20drive%20where%20the%20Inode%20stores%20the%20data.) associated with the file
    | |  |  | └--------------------------------------------- Alternative access (blank means none defined, anything else varies)
    | └--└--└----------------------------------------------- Read, write and special access modes for [U]ser, [G]roup, and [O]thers (everyone else)
    └------------------------------------------------------- File type flag. This is a normal file, so the type is **-**. It could be of type **l**, among others, which would mean that the file is a symbolic link (a.k.a. shortcut). **d** is for directories. [Here](https://linuxconfig.org/identifying-file-types-in-linux) is more information about file types
    

    If we wanted to print the info of a directory, we would get something like this:

    drwxrw-r--    16    root   root 2048    Feb 21 10:57 downloads
    ?UUUGGGOOOS   00  UUUUUU GGGGGG ####    MON DD XX:XX DIRECTORY
    ^ ^  ^  ^ ^    ^      ^      ^    ^      ^            └  Directory name
    | |  |  | |    |      |      |    |      └-------------- Time of last modification
    | |  |  | |    |      |      |    └--------------------- File size. Some versions and/or command line flags will list the number of blocks used instead of the number of bytes. A filesystem with a block size of 1024 bytes will list all sizes up to 1024 bytes as 1, meaning 1 block is used; from 1025 to 2048 as 2, using 2 blocks; and so on. However, listing block sizes by default (without explicitly using a command line option) is rare on most modern Unix machines
    | |  |  | |    |      |      └-------------------------- Group Name (for example, Users, Administrators, etc)
    | |  |  | |    |      └--------------------------------- Owner's name
    | |  |  | |    └---------------------------------------- The default link count of any directory is 2. The extra count is because for every directory created, a link gets created in the parent directory to point to this new directory. In order to know how many subdirectories has a directory, the operation would be the number of link counts minus 2. In this case, we can see that we have 14 subdirectories in the downloads directory
    | |  |  | └--------------------------------------------- Alternative access (blank means none defined, anything else varies)
    | └--└--└----------------------------------------------- Read, write and special access modes for [U]ser, [G]roup, and [O]thers (everyone else)
    └------------------------------------------------------- Type flag. **d** stands for directory, which is the only available type for a directory (exceptions may apply)
    

Commands:

  • ls for displaying files/directories of a directory. Stands for list.
  • ls -l for displaying all files information in a directory. Output:
    -rw-rw-r-- 1 jc jc 192 abr 13 14:18 foo.txt
    -rw-rw-r-- 1 jc jc   0 abr 13 14:56 bar
    
  • ls -l [FILE] for displaying all the information of the specified file. Output if the input is ls -l foo.txt: -rw-rw-r-- 1 jc jc 192 abr 13 14:18 foo.txt
  • ls -d ????
  • ls [-a|--all] for displaying the name of the directories and files in a directory, including the current directory (.) and the parent directory (..). Example: . .. bar foo.txt
  • ls [-al|-a -l] for displaying all information of the files/directories in a directory, including the current directory (.) and the parent directory (..).
    drwxr-xr-x  2 jc jc 4096 abr 13 14:56 .
    drwxr-xr-x 16 jc jc 4096 abr  8 14:16 ..
    -rw-rw-r--  1 jc jc    0 abr 13 14:56 foo
    
  • ls -ltfor displaying all information about files/directories in date order (new to old).
  • ls -ltrfor displaying all information about files/directories in reverse date order (old to new).
  • ls can also be combined with headand tail (explained below) like this: ls -ltr | tail -3. That would show the 3 most recently updated files.
  • pwd for showing the path of the current directory. Stands for print work directory.

Display a file in the CLI

cat

  • cat [FILE] … for displaying an entire file. Stands for concatenate.
  • cat > [FILE] for creating a new file along with its content. Similar to touch. E.g:
    jc@jc:~/Downloads$ cat > foo.txt
    I'm just
    a file
    :D
    
    If we want to save the file, then press Ctrl + D. If not, press Ctrl + C.
  • cat >> [FILE] for appending text at the end of a file.
  • cat [-n|--number] [FILE] for displaying the line numbers next to each line in the terminal, including blank lines.
  • cat [-b|--number-nonblank] [FILE] for displaying the line numbers next to each line in the terminal, excluding blank lines.
  • cat [-s|--squeeze-blank] [FILE] for squeezing repeating blank lines into one.
  • cat [FILE] | wc -c for displaying the total number of characters in a file.
  • cat [FILE 1] [FILE 2] … > [FILE 3] for concatenating the content of [FILE 1] and [FILE 2] in [FILE 3].
  • cat [FILE] | head -[NUMBER] for outputing the first [NUMBER] of a file.
  • cat [FILE] | tail -[NUMBER] for outputing the first [NUMBER] of a file.

more

  • more [FILE] for showing the content of a file. It is similar to cat but, if the file is large, it will display the percentage opened of the file. For displaying more lines, it is necessary to press Enter.
  • more -[NUMBER] [FILE] for limiting the number of lines showed.
  • more -s [FILE] for squeezing repeating blank lines into one. Same as cat -s [FILE].
  • more +/[STRING] [FILE] for printing the fragment in which the given string appears.

less

  • less is an advanced version of more. It allows us to go backward and forward. Its commands are based in vi, from which less differs in its speed, because it does not need to read the entire file before opening it. If the file is large, it will be divided into pages. The end of a page is marked by a :.
    • If we want to go a page forward, press F or Space. To go backward, press B.
    • To close a file, press Q.
  • less [FILE] for opening a file.
  • less [-n|-N] [FILE] for opening a file and showing line numbers.
  • less -X [FILE] to avoid opening a file in a new screen. It will print the file in the command line instead, as more does.
  • less +F [FILE] for watching the file contents for changes. Useful when opening log files that are being written. Similar to tail -f.

head & tail

  • head [FILE] for showing the first 10 lines of a file.
  • head -[NUMBER] [FILE] or head -n [NUMBER] [FILE] for showing the first [NUMBER] lines of a file.
  • tail [FILE] for outputing the last 10 lines of a file.
  • tail -[NUMBER] [FILE] … or tail -n [NUMBER] [FILE] for showing the last [NUMBER] lines of a file. We can also execute [head|tail] -n [NUMBER] [FILE] … for printing the first/last line of each argument.
  • [head|tail] [-f|--follow] [FILE] for watching the file contents for changes. It will print the first/last 10 lines of the file.

Create

  • touch [FILE] for creating a new file. E.g. touch /etc/foo.txt.
  • mkdir [DIRECTORY] for creating a directory. Stands for make directory.
  • mkdir -p [DIRECTORY] for creating a subdirectory. It is super useful when we want to create the subdirectory of a directory that we have not created yet. E.g. mkdir -p /downloads/foo/bar where foo has not been created yet.

Move

  • mv [FILE] [DIRECTORY] for moving a file to a certain directory. Stands for move.
  • mv [OLD FILE] [NEW FILE] for renaming a file. The [OLD FILE] is the file that will be renamed with a new name [NEW FILE]. E.g. mv foo.txt bar.txt

Remove

  • rm [FILE] for removing a file. Stands for remove.
  • rm [-f|--force] [FILE] for forcing the removal of a file. Stands for remove.
  • rm [-r|-R|--recursirve] [DIRECTORY] for removing a directory recursively. They all do the same.
  • rm [-rf|-r -f|-R -f|--recursive --force] [DIRECTORY] for forcing the removal of a directory recursively. They all do the same.
  • shred [FILE] for overriding the content of a file repeatedly to make it extremely difficult to recover.
  • shred -u [FILE] for deleting the file after overriding it.

Copy

  • cp [FILE TO COPY] [NEW FILE] for copying a file. E.g. cp /downloads/foo.txt /documents/bar.txt. Stands for copy.
  • cp [-r|-R|--recursive] [DIRECTORY TO COPY] [NEW DIRECTORY] for copying a directory.
  • cp [-rT|-RT] [DIRECTORY TO COPY] [NEW DIRECTORY] for copying only the files and subdirectories but not the source directory.
  • cp [FILE] [DIRECTORY] for copying a file into a given directory.
  • cp [FILE|DIRECTORY 1] [FILE|DIRECTORY 2] … [FINAL DIRECTORY] for copying multiple files and directories at once to a specific directory. The last argumment must be the target directory.
  • cp [-i|--interactive] [FILE|DIRECTORY] [FILE|DIRECTORY] for forcing cp to prompt for confirmation.
  • cp [-n|--no-clobber] [FILE TO COPY] [NEW FILE] for not overwriting an existing file.
  • cp [-u|--updating] [FILE TO COPY] [NEW FILE] for copying the file only if it is newer than the destination.
  • cp [-p|--preserve] [FILE TO COPY] [NEW FILE] for preserving the file mode, ownership and timestamps from the old file to the new one.
  • cp [-v|--verbose] [FILE|DIRECTORY TO COPY] [NEW FILE|DIRECTORY] for printing verbose output.

Search

grep

  • grep [PATTERN] [FILE] for printing the occurences of the pattern in the file. E.g. grep "chair" foo.txt. Stands for global regular expression print. Bear in mind that, if we execute grep "ng" foo.txt, a few possible matches are running, searching, falling… If you want to look for a matching word, see the flags -w or --word-regexp.
  • grep [-r|--recursive] [PATTERN] [DIRECTORY] for searching the pattern in a directory, skipping the symlinks.
  • grep [-R|--dereference-recursive] [PATTERN] [DIRECTORY] for searching the pattern in a directory, following the symlinks.
  • grep [-c|--count] [PATTERN] [FILE] for retrieving the number of occurences of the given pattern found in the file.
  • grep [-v|--invert-match] [PATTERN] [FILE] for displaying the lines that do not match the pattern.
  • grep [-l|--files-with-matches] [PATTERN] [FILENAME WITH WILDCARD] for showing only the names of the files in which the pattern is found. E.g. grep -l "hello world" *.conf.
  • grep [-i|--ignore-case] [PATTERN] [FILE] for executing a case insensitve search.
  • grep [-w|--word-regexp] [PATTERN] [FILE] for retrieving an exact match of the pattern.
  • grep [-n|--line-number] [PATTERN] [FILE] for showing the match along with the line number in which it has been found.
  • Bear in mind that we can use regular expressions instead of hardcoded strings. Check this link out for more information.

find

Before getting into more details, it is important to know the different types available:

  • f: a regular file
  • d: directory
  • l: symbolic link
  • c: character devices
  • b: block devices
  • p: named pipe (FIFO)
  • s: socket

The most common are f and d.

By name

  • find [DIRECTORY] -type f -name '[PATTERN]' for searching a file by name. E.g. find /documents -type f -name 'foo.txt'. The name of the file can be surrounded by either single or double quotes as well (which is prefered to avoid errors).
  • find [DIRECTORY] -type f -iname '[PATTERN]' for searching a file by name, regardless of its case. E.g. find /documents -type f -name 'foo.txt'.

By extension

  • find [DIRECTORY] -type f -name '*.[EXTENSION]' for searching files by their extension. E.g. find /documents -type f -name '*.txt'.
  • find [DIRECTORY] -type f -name '*.[EXTENSION]' for searching files by their extension. E.g. find /documents -type f -name '*.txt'.

By size

  • find [DIRECTORY] -type f -size [NUMBER][UNIT] for searching a file that fits the size criteria. E.g. find /downloads -type f -size 1024c will look for files of exactly 1024 bytes in the downloads directory.
  • find [DIRECTORY] -type f -size [+|-][NUMBER][UNIT] for searching a file above or below the size criteria.

Examples:

  • find . -type f -size +1M for searching a file in the current directory above with a size above 1 Megabytes.
  • find . -type f -size -1k for searching a file in the current directory above with a size below 1 Kilobytes.
  • find . -type f -size +10M -size 100M for searching a file in the current directory in the range of 10 Megabytes and 100 Megabytes.

The units can be:

  • b: 512-byte blocks (default)
  • c: bytes
  • w: two-byte words
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

By date

  • find [DIRECTORY] -name [FILE] -mtime [NUMBER OF DAYS] for searching modified files in the last [NUMBER OF DAYS]. E.g. find /downloads -name "*.jpg" -mtime 5.
  • We can use [+|-]as well, just like this: find /music -mtime +30 -daystart. This will list all files in the music directory that were modified 30 or more days ago.

By permission

  • find /var/www/html -perm [PERMISSION IN OCTAL] for searching by permission. E.g. find /pictures -perm 644.
  • In front of the [PERMISSION IN OCTAL], we can place a - for excluding the permission or a /, which means that, at least, one category (user, group, or others) must have the respective bits set for a file to match. E.g. find . -perm /222 will match all the files with write permissions set for either user, group, or others.

By owner

  • find [DIRECTORY] -user [USER] for finding files owned by a particular user.
  • find [DIRECTORY] -group [USER] for finding files owned by a particular group.

**Find and execute a file

  • find [FILE|DIRECTORY] -type [TYPE] -exec [COMMAND] for executing a command in the given file/directory.

Find and delete a file/directory

  • find [FILE|DIRECTORY] -type [TYPE] [PATTERN] -delete for deleting a given file/directory. Bear in mind that find can only delete empty directories. If you want to delete a directory with content, use rm [-r|-R|--recursive].

Reverse search

  • The -not option allows us to find files/directories that do not contain the given pattern. E.g. find /documents -type f -not -name 'foo.txt' will retrieve those files that are not named as foo.txt.

Word count

  • wc [FILE] … for showing the number of characters, lines, size and name of the file(s). Output example if we execute wc foo.txt: 37 207 1000 foo.txt.
  • wc < [FILE] … for showing the number of characters, lines and size of the file(s).
  • wc [-l|--lines] [FILE] … for printing the number of lines of the file(s).
  • wc [-w|--words] [FILE] … for printing the number of words of the file(s).
  • wc [-m|--chars] [FILE] … for printing the number of characters of the file(s).
  • wc [-c|--bytes] [FILE] … for printing the number of bytes of the file(s).
  • wc [-L|--max-line-length] [FILE] … for printing the length of the longest line.
  • wc [FLAG] --files0-from=F for reading an input from the files specified by NUL-terminated names in file F. If F is - then read names from standard input. NUL-terminated filenames are the output of another program. For example, the find utility normally returns results separated by newlines but one can use the -printf0 flag to have it return results separated by NUL. This is useful when passing filenames with spaces to another program, like find, which treats white space a separators by default.
    Output example if we execute find /documents -name '*.txt' -printf0 | wc -l --files0-from=-:
    5 /documents/foo.txt
    2 /documents/bar.txt
    7 total
    

Text editors

  • vi [FILE] for opening a file with Vi. Stands for Visual.
  • vim [FILE] for opening a file with Vim. Stands for Vi improved.
  • nano [FILE] for opening a file with Nano.
  • gedit [FILE] for opening a file with Gedit (similar to Notepad++).

Permissions

Before going into more details on them, is important to understand the meaning of each letter in ugo:

  • user owner: The user who owns a directory or file.
  • group members: Users in the directory or file group.
  • other users: Other users not in the directory or file group.

Beyond those three letters, there is the letter a, which stands for all users. It is the equivalent of ugo, executed altogether. For example, chmod ugo+rwx foo.txt is the same as chmod a+rwx.

Reference

We also need to know that rxw stands for:

  • read if permitted. - if it is not.
  • write if permitted. - if it is not.
  • execute if permitted. - if it is not. If applied in directories, it means that it can be opened.

Commands:

chown

  • chown for changing the owner of file system files or directories. Stands for change owner. It is important to note that normal users can change the group of the file only if they own the file and only to a group of which they are a member. Administrative users can change the group ownership of all files.
  • chown [USER NAME|UID] [FILE] … for changing the ownership of a file/directory to a new owner. It accepts multiple arguments for changing the ownership of multiple files/directories at once. E.g. chown jc foo.txt bar.txt and chown 1000 /pictures. UID stands for User Id, which can be displayed by the command id.
  • chown :[GROUP|GID] [FILE] … for changing the ownership of a file/directory to a new group. GID stands for Group Id.
  • chown [USER|UID]:[GROUP|GID] [FILE] … for changing the ownership of a file/directory to a new user and group.
  • chown [-h|--no-dereference] [USER|UID]:[GROUP|GID] [SYMLINK] for changing the ownership of the symlink itself.
  • chown --reference=[FILE OF REFERENCE] [FILE] … for changing the user and group ownership of given files to be same as those of the [FILE OF REFERENCE]. E.g. chown --reference=/pictures/foo.txt bar.txt.
  • Remember to add the [-R|--recursive] flag for apply the commands above to directories, such as this: chown -R jc:group1 /downloads.

chmod

  • chmod for changing the access permissions of file system objects (a.k.a. modes). Given permissions are in octal. Stands for change mode.

    Common permissions:

    • +[r|w|x]: Same as chmod ugo+[r|w|x] [FILE]. Grants the specfied permission(s). E.g. chmod +x foo.txt, chmod ugo+rx foo.txt, chmod u+r foo.txt.
    • =[r|w|x]: Same as chmod ugo=[r|w|x] [FILE]. Causes the permission(s) to be added and causes unmentioned bits (permissions) to be removed except that a directory's unmentioned set user and group ID bits are not affected. E.g:
      $ touch foo.txt ; ls -l foo.txt
      -rw-r--r-- 1 jc jc 0 Jul 12 05:23 foo.txt*
      $ chmod u+x foo.txt; ls -l foo.txt
      -rwxr--r-- 1 jc jc 0 Jul 12 05:23 foo.txt*
      $ chmod u=x foo.txt; ls -l foo.txt
      ---r--r-- 1 jc jc 0 Jul 12 05:23 foo.txt*
      
    • -[r|w|x]: Same as chmod ugo-[r|w|x] [FILE]. Revokes the specified permission(s). E.g. chmod -r foo.txt, chmod ug-x foo.txt, chmod uo-rw foo.txt.
    • As one can expect, we can combine operations in the same command like this: chmod u=rw,g-r,o+x foo.txt.

A few octal notation permissions:

  • 777: rwx for everyone. Same as chmod ugo=rwx E.g. chmod 777 [FILE] chmod 777 -R [DIRECTORY]. Symbolic notation: -rwxrwxrwx
  • 770: rwx permissions for user owner and group. Same as chmod ug=rwx [FILE].
  • 755: rwx permissions for the user owner and rx permission for the group members and others. Same as chmod u=rwx,go=rx [FILE].
  • 700: rwx for user owner. Same as chmod u=rwx [FILE]. Symbolic notation: -rwx------.

These are a few examples but, how can we form our own octal notation permission? Well, it is as simple as this:


If we wanted to know the octal notation of u=rx,g=x,o=rw then we know that the user owner permissions are translated into 5, then the group permission into 1 and, finally, the others permissions into 6.

Another mnemotecnichal rule for remembering this is to associate that r=4, w=2 and x=1. However, it is necessary to understand from where these numbers come from:

  • r is 4 in decimal because it is on the third bit from the right (r--). It's 1*2²=4.
  • w is 2 in decimal because it is on the second bit from the right (-w-). It's 1*2¹=2.
  • x is 1 in decimal because it is on the first bit from the right (--x). It's 1*2⁰=1.

DIRECTORY TRAVERSE

  • cd .. for going up one level of the directory tree. Stands for change directory.
  • cd [DIRECTORY] for opening to a certain directory.
  • cd ~ or just cd for going to the HOME directory.
  • cd -for going to the previous directory.

NETWORK

  • host for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options.
  • ping [DOMAIN|IP] for checking whether a network is available and if a host is reachable.
  • dig [DOMAN|IP] for querying DNS name servers. Stands for Domain Information Groper.
  • ifconfig for displaying current network configuration information, setting up an ip address, netmask, or broadcast address. Stands for interface configuration.
  • ftp for connecting to a FTP server.
  • lftp for connecting to a FTP or SFTP server. It has to be installed via apt with the command sudo apt install lftp.

wget

  • wget [URL] for retrieving content from the Internet. Stands for World Wide Web Get.
  • wget [-O|--output-document=] [NAME] [URL] for saving the downloaded file under a different name instead of its original name.
  • wget [-P|--directory-prefix=] [DIRECTORY] [URL] for saving the file to a specific location. Bear in mind that, by default, wget will download the file in the current working directory.
  • wget --limit-rate=[RATE][k|m|g] [URL] for limiting the download speed to the specified [RATE]. The rate can be in kilobytes, megabytes and gigabytes. Example: wget --limit-rate=5m https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz.
  • wget [-c|--continue] [URL] for resuming a download. Notice that, if the remote server does not support resuming downloads, wget will start the download from the beginning and overwrite the existing file.
  • wget [-b|--background] [URL] for downloading the file in the background.
  • wget [-U|--user-agent=] "[USER AGENT]" [URL] for emulating a different browser, in case that the remote server block the Wget User-Agent. E.g. wget --user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" http://wget-forbidden.com/.
  • wget [-i|--input-file] [-|FILE] for downloading multiple files at once from a file (if we enter the name of a file) or from the standard input (if we enter a dash).
  • wget [-m|--mirror] [URL] for creating a complete local copy of a website by following and downloading all internal links as well as the website resources (HTML, JavaScript, CSS and images).
  • wget [-mkp|-m -k -p] [URL] for downloading a website for local browsing. The -k flag will cause wget to convert the links in the downloaded documents to make them suitable for local viewing. The -p flag will tell wget to download all necessary files for displaying the HTML page.
  • wget --no-check-certificate [URL] for downloading a file over HTTPS from a host that has an invalid SSL certificate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment