Basic Commands in Linux

Basic Commands in Linux

Some of the basic commands in Linux that you will use from the shell are as follows:

  1. pwd
  2. ls
  3. cp
  4. mv
  5. touch
  6. rm
  7. echo
  8. ln

1. pwd

The pwd command will display the name of the current directory.

$ pwd
/home/tux

2. ls

The ls command lists the contents of the directory.

$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

For a long listing format, use the -l option:

$ ls -l
total 32
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Desktop
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Documents
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Downloads
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Music
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Pictures
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Public
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Templates
drwxr-xr-x 2 tux tux 4096 Jan 14 09:01 Videos

3. cp

The cp command lets you copy files and directories.

$ cp file1 ~/Downloads ~/Documents

This means copying file named file1 from Downloads folder to the Documents folder.

4. mv

The mv command serves two purposes: (1) moving a file/directory and (2) renaming a file/directory.

$ mv file2 ~/Documents/     #1
$ mv file3 file3.txt        #2
  • In #1, I moved file2 to the ~/Documents folder.
  • In #2, I renamed file3 to file3.txt.

5. touch

One of purpose of touch command is to create a file without content.

$ touch file4

file4 is created without any content inside.

6. rm

The rm command let's you remove files or directories.

When you no longer wanted file4, let's remove it by:

$ rm file4

To forcefully remove the file4, do:

$ rm -f file4

To remove empty directory:

$ rm -d dir4

To remove directories and their contents recursively:

$ rm -r dir4

7. echo

The echo command will display a line of text.

$ echo "Hello World!"
Hello World!
$ echo $USER
tux

8. ln

The ln command lets you create links between files. By default, it makes hard links.

8.1. Hard links

Every file on the Linux system will have at least one hard link.

$ pwd
/home/tux/Documents

I have the following directories and files inside the Documents folder:

.
├── data-2024
└── report-2024
    └── q1-2024.txt

2 directories, 1 file

So, if I want to create a hard link named q1-2024.txt inside the data-2024 directory, I can achieve this with the following command:

$ ln report-2024/q1-2024.txt data-2024

Now, if I see my Documents directory, I will have:

.
├── data-2024
│   └── q1-2024.txt
└── report-2024
    └── q1-2024.txt

2 directories, 2 files

Here, both q1-2024.txt files are identical to each other, hence they are 'hard-linked', and when one gets updated, the other file will be updated as well.

8.2. Soft links

Soft links in the Linux system are similar to 'shortcuts'. In the above-mentioned example, let's say you want to create a shortcut of q1-2024.txt in the /home/tux/Manager folder. We can use the following command:

$ ln -s ~/Documents/report-2024/q1-2024.txt ~/Manager

Or, use the full-path

$ ln -s /home/tux/Documents/report-2024/q1-2024.txt /home/tux/Manager

Now, you will see the following output when you do a long listing to the Manager folder:

$ ll ~/Manager
total 8
drwxrwxr-x  2 tux tux 4096 Jan 14 10:22 ./
drwxr-xr-x 17 tux tux 4096 Jan 14 10:21 ../
lrwxrwxrwx  1 tux tux   43 Jan 14 10:22 q1-2024.txt -> /home/tux/Documents/report-2024/q1-2024.txt

Observe the last line, there are couple of things to notice:

  1. Soft link is represented with letter l, i.e., lxxxxxxxxx.
  2. q1-2024.txt is pointed to /home/tux/Documents/report-2024/q1-2024.txt by -> symbol.

This post is part of the series called A Linux Crash Course for Absolute Beginners.

Leave a Reply

Your email address will not be published. Required fields are marked *


© 2024 A. Maharjan