If you’re new in Linux, these commands will help you to start learning the system, specially the command line.

  • pwd: show the working directory.
  • cd <path>: change the working directory (cd files/). If you don’t add a <path>, it will change to the “home” directory for the user (run echo $HOME to see what is your home directory).
    • cd ..: change to the parent folder.
  • ls <path>: list files and folders. <path> is optional if you want to list files inside your working directory. Otherwise, you can use relative paths (relative to your working directory, like ./folder/, ../folder/) or absolute paths (like /home/user/).
    • ls -l: show files and folders permissions.
    • ls -a: include hidden files (files that start with .).
    • ls -lt: order by last modified time, ls -ltu: order by last accessed time.
    • ls --color=auto: colorizes the output.
  • du <path>: similar to ls, list files/directories and their sizes. Unlike ls, du shows folder size correctly (as a sum of the sizes of all their files).
    • du -h: use human-readable size format.
    • du --max-depth=<number>: limit the recursive listing to <number> levels.
    • du -a: include files in the list (not only directories).
    • du --apparent-size: print apparent sizes rather than device usage (useful for small files). You can use it with --block-size=1 for printing bytes instead of kilobytes.

Copy, move and delete content

  • cp <source> <target>: copy a file from a <source> to a <target>: For example, cp ./file.txt ./folder/. You can change the filename by typing the new filename in <target> (cp ./file.txt ./folder/file2.txt).
    • cp -r <folder> <target>: copy files and folders recursively.
  • mv <source> <target>: move a file or folder from <source> to <target>. It moves recursively.
  • rm <file>: remove a file.
    • rm -r <folder>: remove files and folders recursively.

Create and delete folders

  • mkdir <folder>: create a folder.
  • rmdir <folder>: remove an empty folder.

Processes

  • <command> &: run a command in the background.
  • pkill <process name>: kill a process by its name.
  • kill <process PID>: kill a process by its PID (Process ID).
  • ps ax: list running processes owned by any user.
  • ps aux: similar to the above, with more info.
  • top: similar to ps ax, but updates automatically. Most people use htop (an enhanced version of top) for this task.
  • which <command name>: find where a command is being run from.
  • free: provides information about physical memory and swap space. By default, displays numbers in kilobytes. Add -m to show numbers in megabytes.

Block devices and filesystems

  • lsblk: list available block devices.
  • df: displays disk usage. Add -h to show disk space in a human-readable format.
  • du: shows the amount of space that is being used by files in a directory. Add -h to show disk usage in a human-readable format.

Hardware information

  • lspci: list PCI devices. Add -v to get more info.
  • lsusb: list USB devices.
  • lscpu: info about CPU.

Pipes / redirections

  • command1 | command2: transform the output from command1 into the input of command2.
    cat /etc/passwd | grep root
    
  • command > file: create (or overwrite) a file with the output from command
    find . -maxdepth 1 > files.txt
    
  • command >> file: append the output from command into file (in a new line). It does not overwrite the file if it exists but it will create it if it does not exist.
  • command < file: use the content of file as input to command.
  • command 2>file: redirect standard error from command to file.
    ls /somefolder > file.txt 2>&1
    
    • Redirect standard output and standard error to the same file.txt.
    for i in {0..20}; do ls test_$i.txt 1>/dev/null; done
    
    • Show only the files that do not exist in that range (1 refers to standard output, 2 to standard error).
  • command << EOF: this allows to pass multiline text to a command. EOF could be any word you can use to define the end of the text.
    $ cat > multiline.txt << EOF
    > one
    > two
    > three
    > EOF
    

More

  • clear or Ctrl + L: clear the terminal.
  • Ctrl + +: increase the terminal font size.
  • Ctrl + -: decrease the terminal font size.
  • head <file>: show the first 10 lines of a file. You can specify any number of lines with -n <number>.
  • tail <file>: show the last 10 lines of a file. You can specify any number of lines with -n <number>.
  • read -p "<prompt>" <variable>: read from user input and assign to a variable.
    $ read -p "Confirm (y/n): " res; echo $res 
    Confirm (y/n): y
    y
    
  • journalctl: shows system logs. You need superuser privileges to see all logs.
    • Add -n <number> to show last <number> logs.
    • -f shows a live list of more recent logs.
    • -p <priority>: shows logs with that priority or higher (e.g.: journalctl -p warning).