4.2. Basic commands#

This section provides an overview of some commands that are used frequently on Linux systems.

Familiarity with these will help you navigate the file system and work with files, and directories.

4.2.1. A note on command options#

A command might support additional options.

For example, the command:

ls

— prints a list of files in a directory, whereas:

ls -a

— will also print hidden files in that directory.

4.2.2. Getting help with commands#

In most cases, you can see the complete list of options a command supports using --help:

ls --help

Output:

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .

...

Note

To keep this discussion short, the examples in this section, only includes some options supported by a command.

4.2.3. Display current date and time: date#

date

Output:

Wed 10 Mar 2021 10:33:11 GMT

4.2.4. Change directory: cd#

To change into a directory, use cd followed by the name of that directory.

For example, to change into /home/user/Downloads, use:

cd Downloads

Note

When you log in or open a new terminal window, you will be placed in your home directory — /home/user, where user is your username.

You can verify your current directory using the pwd command discussed earlier:

pwd
/home/user/Downloads

cd with no arguments#

No matter where you are in the file system, typing cd without any arguments will take you to your home directory.

user@cookbook:/usr/share/dict$ cd
user@cookbook:~$

Go to the parent directory#

Use cd .. to go to the parent directory.

For example, when you are in /home/user/Downloads and type:

cd ..

You will be taken to /home/user.

A single dot (.) refers to the current directory.

4.2.5. Create new directory: mkdir#

Use mkdir followed by the name of the directory you would like to create:

mkdir Workspace

Attention

The directory you’re creating must not already exist or this command will not work.

As discussed earlier, directory names are case-sensitive, so a directory named Workspace is different from another named workspace.

Space in directory names#

If there are spaces in the directory name, use double quotes, for example:

mkdir "Project Work"

Without that, two separate directories — Project and Work will be created.

4.2.6. Remove empty directory: rmdir#

Use rmdir followed by the name of the directory you would like to remove:

rmdir Workspace

Attention

This directory must be empty before it can be removed.

There should be no other files or directories within that directory.

4.2.7. List files and directories: ls#

To list files in a directory, use the ls command:

ls

Output:

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

Listing hidden files#

You can provide additional options to ls. It will change how the command works.

For example, to display hidden files and directories too, use:

ls -a

Output:

.              .cache     Downloads    .local    Public
..             .cinnamon  .gnupg       .bash_history  .config
.bash_logout   Desktop    Pictures     .var      Videos
.bashrc        Documents  .linuxmint   .profile  .Xauthority

Getting a detailed file listing#

To get a detailed listing of files, use:

ls -l

Output:

total 32
drwxr-xr-x 2 user user 4096 Feb 17 11:13 Desktop
drwxr-xr-x 2 user user 4096 Mar  4 11:41 Documents
drwxr-xr-x 2 user user 4096 Feb 17 11:13 Downloads
drwxr-xr-x 2 user user 4096 Feb 17 11:13 Music
drwxr-xr-x 2 user user 4096 Mar  4 11:27 Pictures
drwxr-xr-x 2 user user 4096 Feb 17 11:13 Public
drwxr-xr-x 2 user user 4096 Feb 17 11:13 Templates
drwxr-xr-x 2 user user 4096 Feb 17 11:13 Videos

Combining multiple options#

You can combine multiple options like this:

ls -la

Output:

total 144
drwxr-xr-x 19 user user  4096 Mar 12 10:00 .
drwxr-xr-x  3 root root  4096 Feb 17 10:51 ..
-rw-------  1 user user   190 Mar  4 10:32 .bash_history
-rw-r--r--  1 user user   220 Feb 17 10:51 .bash_logout
-rw-r--r--  1 user user  3771 Feb 17 10:51 .bashrc
drwx------ 17 user user  4096 Mar  3 19:28 .cache
drwxrwxr-x  4 user user  4096 Feb 17 11:40 .cinnamon

To see the list of all options that ls supports, use:

ls --help

4.2.8. Copy files and directories: cp#

With the cp command, you can copy files or directories from one place (source) to another (destination).

Copying one file#

To copy one file, use the following format:

cp source_file destination

Where source_file is the file you would like to copy and destination can either be a file name or a directory.

If destination is a file name#

The copied file will have that file name.

For example:

cp /usr/share/dict/words /home/user/Documents/dictionary.txt

This will copy the words file from /usr/share/dict/ to /home/user/Documents/ and save it as dictionary.txt.

If destination is a directory#

The file will be copied into that directory with the same file name.

For example:

cp /usr/share/dict/words /home/user/Documents

This will copy the words file from /usr/share/dict/ to /home/user/Documents/ with the same file name.

Copying multiple files#

To copy multiple files, use the following format:

cp source_file1 source_file2 destination

Where source_file1 and source_file2 are the files you would like to copy. You can have any number of source files. Here, destination is the directory where you would like to copy source files into.

For example:

cp /usr/share/dict/words /usr/share/dict/spanish /home/user/Documents

This will copy the words and spanish dictionary files from /usr/share/dict/ into /home/user/Documents.

Copying directories#

You can copy entire directories using the -r (recursive) option.

For example:

cp -r /usr/share/dict /home/user/Documents

This will copy the /usr/share/dict directory and its contents to the Documents directory.

4.2.9. Display file contents or combine files: cat#

With the cat command, you can view the contents of a file on the screen or combine contents of multiple files.

View contents of a file#

To view the contents of a file, use cat, followed by the name of the file:

cat /usr/share/dict/words
A
A's
AMD
AMD's
AOL
AOL's
AWS
AWS's
Aachen
...

This will display the entire contents of the file.

Combine contents of files#

If you include multiple file names in cat, it will combine them and print their contents on the screen.

If you would like to save the output to a file instead, use the following format:

cat file1.txt file2.txt > files.txt

The > operator redirects standard output to the file name following it.

4.2.10. View file contents: less#

With the less command, you have more control when viewing a file. It allows you to scroll up and down the file and provides features like searching the file using keywords.

To view the contents of a file, use less, followed by the name of the file:

less /usr/share/dict/words

When the file is open, it will appear as in Fig. 78.

../../_images/less.png

Fig. 78 The words file open in less command#

At the : prompt, you can type commands that less will understand.

Searching the file#

You can search a file while viewing it in less.

To do this:

  1. Type / (forward slash) key

  2. Type the text you would like to search

  3. Press the ENTER key

  4. If there are matching results, they will be highlighted

Quitting the less command#

To quit the less command, type q at the prompt.

4.2.13. Move (rename) a file or directory: mv#

With the mv command, you can move a file or directory from one location (source) to another (destination). You can choose to keep the existing file or directory name or rename them.

The basic format of the command is:

mv source destination

A safer approach is to add the -iv options to the command:

mv -iv source destination

With -i (interactive), mv will require your confirmation before overwriting a file or directory, if it exists already in destination.

-v (verbose) will print the command’s actions on the screen.

For convenience, you can add an alias.

Sample files#

To follow the examples below, you will need to copy the following files into your home directory using the cp command:

cp -v /usr/share/dict/words ~
cp -rv /usr/share/doc/bash ~

~ is a shortcut for home directory.

Moving a file or directory#

The simplest use case is to move a file or directory from one location (directory) to another.

Moving a File#

For example, to move the words file copied above into your Documents directory, use:

mv -iv words Documents/

Output:

renamed 'words' -> 'Documents/words'

Moving a directory#

Similarly, to move the bash directory copied above into your Documents directory, use:

mv -iv bash Documents/

Output:

renamed 'bash' -> 'Documents/bash'

In both cases, the file or directory name will not be changed.

Renaming a file or directory#

In this case, you would like to rename a file or directory.

Renaming a file#

To rename the words file copied above to dictionary.txt, use:

mv -iv words dictionary.txt

Alternatively, to move it into your Documents directory and rename it at the same time, use:

mv -iv words Documents/dictionary.txt

Renaming a directory#

To rename the bash directory copied above into bash-commands, use:

mv -iv bash bash-commands

Notes#

What happens if a file exists?#

You will notice a prompt requesting you for confirmation to overwrite the file.

Type y and press the ENTER key to proceed:

mv: overwrite 'Documents/words'? y
renamed 'words' -> 'Documents/words'

To cancel, simply press ENTER key at the prompt.

What happens if a directory exists?#

mv will overwrite a directory only if it is empty. You can either:

  • copy files into destination directory or

  • rename the destination directory

Adding an alias for mv#

Rather than typing mv -iv, every time you need to use the command, you can add an alias for the command in your ~/.bash_aliases file.

For example:

alias mv='mv -iv'

Now, when you type mv, you will actually be running mv -iv.

4.2.14. Remove files or directories: rm#

With the rm command, you can remove (or delete) files or directories.

The basic format of the command is:

rm source

A safer approach is to add the -iv options to the command:

rm -iv source

With -i (interactive), rm will require your confirmation before deleting a file or directory.

-v (verbose) will print the command’s actions on the screen.

For convenience, you can add an alias.

Sample files#

To follow the examples below, you will need to:

  1. Copy the following files into your home directory using the cp command:

    cp -v /usr/share/dict/words ~
    cp -rv /usr/share/doc/bash ~
    

    ~ is a shortcut for home directory.

  2. Create an empty directory:

    mkdir empty-dir
    

Removing files#

To remove a file, for example, the words file copied above, you can use:

rm -iv words

Output:

rm: remove regular file 'words'? y
removed 'words'

Removing directories#

Removing empty directories#

If the directory is empty, you can remove it using the -d option:

rm -d empty-dir

Alternatively, you can use the rmdir command:

rmdir empty-dir

Removing directories with content#

If the directory has some content i.e., files or subdirectories, you will need to add the -r (recursive) option.

For example, using the bash directory copied above:

rm -ivr bash

This command will ask for your confirmation for deleting every file in the directory and then delete it:

rm: descend into directory 'bash'? y
rm: remove regular file 'bash/RBASH'? y
removed 'bash/RBASH'
...
rm: remove regular file 'bash/README.gz'? y
removed 'bash/README.gz'
rm: remove directory 'bash'? y
removed directory 'bash'

Instead of -i, you could use the -I option, which will only prompt once, when removing directories recursively:

rm -Ivr bash

Output:

rm: remove 1 argument recursively? y
removed 'bash/RBASH'
...
removed 'bash/README.gz'
removed directory 'bash'

If you are completely sure you do not need the directory and its contents, you can force its removal using the -f option:

rm -vrf bash

rm will delete the directory without confirmation.

Notes#

Adding an alias for rm#

Rather than typing rm -iv, every time you need to use the command, you can add an alias for the command in your ~/.bash_aliases file.

For example:

alias rm='rm -iv'

Now, when you type rm, you will actually be running rm -iv.

4.2.15. Display text or values of variables: echo#

With the echo command, you can print text or the values of environment variables on the screen.

The basic format of the command is:

echo text_or_variable

Printing text#

To print text on the screen, use echo followed by the text you would like to display.

echo "Hello, This is echo!"

Output:

Hello, This is echo!

Printing values of environment variables#

You can also use echo to display values of your environment variables.

For example:

echo $PATH

The $ in PATH indicates, it is a variable.

Output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

PATH contains the list of paths (directories) that will be searched for locating programs or commands.

You can set or modify environment variables in your ~/.bashrc.

4.2.16. View free disk space: df#

The df command will report used and available disk space on all storage devices connected to the system.

For example, to find the amount of disk space used in / — the root filesystem, you can use the command:

df -h /

Output:

Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/vgmint-root   28G  6.9G   20G  26% /

The -h option makes the output human-readable. Without this, available disk space will be reported as 20600196 instead of 20G.

4.2.17. View disk space usage of files or directories: du#

Calculate the amount of disk space used by files or directories.

For example, to calculate the amount of disk space used by files in your home directory, use the command:

du -sh ~

Output:

7.4G .

The -s option prints a summary instead of printing space used by individual files.

-h makes the output human-readable.

4.2.18. View memory (RAM) usage: free#

With free, you can find the amount of free and used memory (RAM) and swap space.

Usage:

free -h

Output:

              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       618Mi       1.3Gi       7.0Mi       1.9Gi       3.0Gi
Swap:         979Mi          0B       979Mi

The -h option makes the output human-readable with corresponding units displayed. In the example above, the amount of free physical memory (RAM) is 1.3 GiB (Gibibytes).

4.2.19. Search for files: find#

Search for files using their file name, file type etc.,

For example, to find files in the home directory with .bash in their name, use the command:

find ~ -name ".bash*"

Output:

/home/user/.bashrc
/home/user/.bash_history
/home/user/.bash_logout

Note

The ~ here is a shortcut for home directory.

The asterisk symbol (*) in .bash* acts as a wildcard.

The find command does not use a database, so the search will be slower when compared to locate. However, it can identify files that were created or modified recently.

4.2.20. Find files using their file names: locate#

Search for files using their file names. This method is quick as it uses a file name database.

For example:

locate bashrc

Output:

/etc/bash.bashrc
/etc/skel/.bashrc
/home/user/.bashrc
...

Note

This method might not find files that were created or modified recently.

Newer files might not yet be included in the database. To update the database manually, you can run the command:

sudo updatedb

Alternatively, you can use the find command to search for new files.

4.2.21. Locate a command: which#

The which command prints the complete path to a command, if it exists in the search path i.e., $PATH.

For example:

which python3

Output:

/usr/bin/python3

Comments