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

Table of Contents

Parameters

A command usually has this structure:

<command_name> <options> <file>

In this case, <options> and <file> are parameters. Command options have an option name and an optional (or required) option value. Option names usually start with a single dash (-) when is a one-letter, or two dashes (--) when the option name is a word. Some GNU commands (like tar) allow to specify one-letter option without typing the dash.

When you want to run a command where the <file> parameter starts with a dash, you need to tell to the command that <file> is not an option name. You can do this by typing -- before <file>. In the following example, we are going to create a file called -testfile:

touch -- -testfile
  • 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/).
    • You can use wildcards to filter the list: ls *.png.
    • 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.
    • rm -f <file>: remove a write-protected file (you need to have write permissions on the folder).

Create and delete folders

  • mkdir <folder>: create a folder.
    • mkdir -p <folder>: create parent folders if they don’t exist.
  • 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 or job number>: kill a process by its PID (Process ID) or job number (prepend ‘%’ in this case) It sends SIGTERM signal.
    • kill -s <signal> <PID>: kill a process with a signal (type kill -l for a signals list). You can also type the signal name (SIGSTOP) or type signal number after a dash (-9).
      kill 1234
      kill %1
      kill -s 15 1234
      kill -s SIGTERM 1234
      kill -9 1234
      
  • killall <process name>: kill all instances of a process.
  • 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.
    • lsblk -o <fields>: customize the output by selecting the columns.
      lsblk -o name,uuid,label
      
  • 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. Add -d <number> to specify a maximum depth.

Hardware information

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

File info

  • file <file>: generic information about file type.
  • stat <file>: information about file permissions and date metadata (last accessed, last changed, etc.)

Pipes / redirections

  • command1 | command2: transform the output from command1 into the input of command2.
    cat /etc/passwd | grep root
    
    • You can specify where to add the output of the first command by using - in the second command.
  • 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
    # same as:
    ls /somefolder &> file.txt
    
    • 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
    

Users

Permissions

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>.
    • head -n -3: show all except the last 3 lines.
  • tail <file>: show the last 10 lines of a file. You can specify any number of lines with -n <number>.
    • tail -n +3: show all except the first three lines.
  • 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
    
  • read -n 1 <variable>: accepts only one character for input.
  • man <page>: show a reference manual about a topic.
    • man <section number> <page>: show an specific section (e.g.: you can access crontab(5) by typing man 5 crontab)
  • date: show date and time.
    • date +FORMAT: shows date/time with the specified format: For example, date +%s shows seconds since the Epoch.
    • date -d <date>: display <date> (date -d 20210101, date -d "2 weeks ago").
  • Ctrl + R: Search through command history. Type a search and press Enter to execute the found command, Ctrl + R to go to next match, or press the right arrow to edit the command.
  • run-parts <folder>: execute all scripts inside a folder, sequentially.
Test with this online terminal: