Lesson 2 of 5 · Linux for DBAs

Linux Commands

The commands you'll use every day to find your way around, create and organize files and directories, and read their contents.

Beginner 12 sections 5 min read

How a command is structured

Most Linux commands follow the same pattern: the command, then options that change how it behaves, then arguments that say what to work on.

Command what to run Option how to run it Argument what to run it on ls -a /home/kofi
"List all items, including hidden ones, in /home/kofi."

Options usually start with a dash (-a). Some commands need no options or arguments at all, such as pwd.

man: read the manual

The man (manual) command explains how other commands work. Whenever you're not sure how to use a command, open its manual:

$ man {command}
$ man ls        # the manual for the ls command

Use the arrow keys to scroll, type / followed by a word to search, and press q to quit. For a shorter summary, many commands also accept --help, as in ls --help.

pwd: where am I?

pwd (print working directory) shows the directory you're currently in. Whenever you feel lost in the filesystem, run it.

$ pwd
/home/kofi

ls: list what's in a directory

ls (list) shows the items in a directory: the current one, or the one you name.

$ ls -{options} {directory}
OptionWhat it does
-aLists all items, including hidden ones
-lLists items in long format, with details such as permissions, owner, size, and date
$ ls          # list items, names only
$ ls -l       # list items in long format
$ ls -al      # list ALL items, including hidden ones, in long format
$ ls /bin     # list the items in /bin

Here's what ls -al looks like. Hidden items start with a dot:

$ ls -al /home/kofi
total 16
drwx------ 3 kofi kofi 4096 Oct 19 09:12 .
drwxr-xr-x 4 root root 4096 Oct  1 14:03 ..
-rw------- 1 kofi kofi  220 Oct 19 09:12 .bash_history
-rw-r--r-- 1 kofi kofi    0 Oct 19 09:11 favsong.txt
drwxr-xr-x 2 kofi kofi 4096 Oct 19 09:10 music

Reading left to right, each line shows the permissions, number of links, owner, group, size in bytes, date last modified, and name. A d at the start means it's a directory.

touch: create an empty file

touch creates an empty file. If the file already exists, touch leaves its contents alone and just updates its "last modified" time.

$ touch {file-name}
$ touch favsong.txt       # create an empty file called favsong.txt

On Linux, a name that starts with a dot is hidden. ls skips it unless you add -a:

$ touch .favsong.txt      # create an empty hidden file

mkdir: make directories

mkdir (make directory) creates a directory.

$ mkdir -{options} {directory-name}
$ mkdir music             # create a directory called music

Add -p to create nested directories (subdirectories) in one step. It creates any parent directories that don't exist yet:

$ mkdir -p music/afrobeats/dancehall
# creates music (if needed), then afrobeats inside it, then dancehall inside that

Without -p, you'd need one command per level:

$ mkdir parent-dir              # create the parent directory
$ mkdir parent-dir/child-dir    # then the child directory
$ mkdir -p parent-dir/child-dir # or do both at once with -p

cd: change directory

cd moves you into another directory.

$ cd {directory-name}
$ cd music        # move into the music directory
$ cd ..           # move up one level, to the parent directory
$ cd ~            # go to your home directory (plain cd does the same)
$ cd ~/music      # go to the music directory inside your home directory

.. always means "the directory above this one", and ~ is a shortcut to your home directory. If your home directory is /home/kofi, then ~/music means /home/kofi/music.

/home/kofi dir1 you start here dir2 you end up here 1. .. go up 2. dir2 go into dir2
cd ../dir2 goes up one level, then down into dir2.

mv: move or rename

mv moves a file or directory, renames it, or both at once.

$ mv {source} {destination}
$ mv file1.txt music/file1.txt   # move file1.txt into the music directory
$ mv file1.txt music/file2.txt   # move file1.txt into music and rename it file2.txt
$ mv file1.txt random.txt        # rename file1.txt to random.txt

cp: copy

cp copies files. The original stays where it is.

$ cp me.txt you.txt              # make a copy of me.txt called you.txt

To copy a directory and everything in it, add -r (recursive):

$ cp -r music music_2            # copy the music directory to music_2

vi: edit a file

vi is the standard Linux text editor. It lets you write in a file. Vim ("Vi IMproved") is a newer, enhanced version, and on many Linux systems typing vi actually opens Vim.

$ vi {file-name}
$ vi file1.txt       # open file1.txt (or create it) for editing

vi has two main modes. It starts in command mode, where keys run commands; you can't type text until you switch to insert mode.

  • Press i to enter insert mode. -- INSERT -- appears at the bottom of the screen.
  • Press Esc to go back to command mode.
  • In command mode, type : followed by a command, then press Enter:
    • :w saves (writes) the file.
    • :q quits, but only if there are no unsaved changes.
    • :wq saves, then quits.
    • :q! quits without saving your changes.

The vi Editor lesson covers this step by step.

cat: show a file's contents

cat prints the contents of a file to the screen.

$ cat {file-name}
$ cat favsong.txt    # show what's in favsong.txt

Key takeaways

  • Commands follow the pattern: command, options, arguments.
  • Stuck? man {command} shows the manual, and pwd shows where you are.
  • ls, cd, mkdir, touch, cp, mv, and cat cover most everyday file work.
  • mkdir -p creates nested directories, and cp -r copies a whole directory.
  • .. means the parent directory and ~ means your home directory.