Basic commands in Linux¶
- Below is the table including the most common commands in Linux operating system
- These commands are gone through more properly in this section
Utility | Description | Usage |
---|---|---|
pwd | Print current working directory | pwd |
cd | Change current working directory | cd directory |
mkdir | Create a new directory | mkdir directory |
mv | Rename or move a file or directory | mv source target |
ls | List directory contents | ls directory |
rm | Remove a file or directory | rm file (use -r with dirs) |
cp | Copy a file or directory | cp source target |
PWD (Print Current Working Directory)¶
- pwd command prints the directory path where the command was executed (current working directory)
testuser@ubuntu-PC:~$ pwd
/home/testuser
- pwd command makes it easier for new users to navigate in Linux file system
-
Important:
- Executed command will always affect the current working directory if not stated otherwise
- The default working directory is user's home directory
- For example, user home directories are located in /home directory by default
- This means that current working directory for testuser /home/testuser at start
-
Example for the usage of pwd command
testuser@ubuntu-pc:~$ pwd
/home/testuser
testuser@ubuntu-pc:~$ su -
Password:
root@ubuntu-pc:~# pwd
/root
root@ubuntu-pc:~# su – testuser
Password:
testuser@ubuntu-pc:~$ pwd
/home/testuser
LS (List the Contents of a Directory)¶
- ls command is used to list the contents of a directory
- If command is issued without target directory, the current working directory content will be listed
- Example where current working directory is listed:
testuser@ubuntu-pc:~$ pwd
/home/testuser
testuser@ubuntu-pc:~$ ls
data sensors.txt
- Figure below presents this directory location
-
Examples where other directory is provided as an argument for the command
-
List root directory content
testuser@ubuntu-pc:~$ ls /
bin dev initrd.img lib64 mnt root snap sys var
boot etc initrd.img.old lost+found opt run srv tmp cdrom
home lib media proc sbin swap.img usr
- List user's home directory content
testuser@ubuntu-pc:~$ ls ~
data sensors.txt
- List the content of /home directory
testuser@ubuntu-pc:~$ ls /home
testuser
- List the content of both user's home directory as well as /home directory
testuser@ubuntu-pc:~$ ls ~ /home
/home:
testuser
/home/testuser:
data sensors.txt
- Listing format can be changed with options
-
Examples of different listing formats
-
List using long listing format with file and directory permissions
testuser@ubuntu-pc:~$ ls -l
total 4
drwxrwxr-x 2 testuser testuser 4096 Aug 20 05:20 data
-rw-rw-r-- 1 testuser testuser 0 Aug 20 05:20 sensors.txt
- List also hidden files and directories starting with . character
testuser@ubuntu-pc:~$ ls -a
.bash_history .bashrc data .profile .sudo_as_admin_successful
.bash_logout .cache .gnupg sensors.txt
- ls command options can also be combined
- For example, ls -la command lists directory contents using long listing format and in addition, all hidden files and directories are included in listing
testuser@ubuntu-pc:~$ ls -la /etc/cron.d
total 1
drwxr-xr-x 1 root root 512 Jul 25 2018 .
drwxr-xr-x 1 root root 512 Sep 29 08:13 ..
-rw-r--r-- 1 root root 102 Nov 16 2017 .placeholder
-rw-r--r-- 1 root root 589 Jun 26 2018 mdadm
-rw-r--r-- 1 root root 191 Jul 25 2018 popularity-contest
CD (Change Directory)¶
- Moving between directories can be done with cd command (change user's current working directory)
-
Directory path will be given as an argument (either as absolute or relative path)
- Absolute path will always be formed starting from the system root (/)
- Relative path will always be formed starting from the current working directory (from the directory where the command was executed)
-
Example: Let's move from user's home directory to /var/log directory
- Using absolute path:
testuser@ubuntu-pc:~$ cd /var/log
- Using relative path:
testuser@ubuntu-pc:~$ cd ../../var/log
- Below is a figure illustrating the location of the source and target directory within the Linux filesystem
- As can be seen from the figure below, with relative path user must first move two directory levels backwards before entering into /var/log directory
-
Important: The absolute path can easily be used from anywhere on Linux file system!
-
cd command arguments are presented in the table below
Argument | Description |
---|---|
No argument | Changes to user's home directory |
~ | Changes to user's home directory |
.. | Moves one step backwards in file system |
Path | Changes to target path |
- | Changes to directory where user was before the previous directory change |
Touch (Change File Timestamps)¶
- touch command is used to create a new file or update the timestamp of an existing file
- Example where a new file is created under user's home directory
testuser@ubuntu-pc:~$ ls
data sensors.txt
testuser@ubuntu-pc:~$ touch testfile.txt
testuser@ubuntu-pc:~$ ls
data sensors.txt testfile.txt
- Example where timestamp of an existing file is updated
testuser@ubuntu-pc:~$ ls -la /tmp
drwxrwxrwx 1 root root 512 Dec 10 2019 test.txt
testuser@ubuntu-pc:~$ touch /tmp/test.txt
testuser@ubuntu-pc:~$ ls -la /tmp
drwxrwxrwx 1 root root 512 Sep 30 12:52 test.txt
MKDIR (Make Directory)¶
- mkdir command is used to create a new directory
- Directory name will be provided as an argument for the command
- Example where data2 directory is created under user's home directory
testuser@ubuntu-pc:~$ ls
data sensors.txt testfile.txt
testuser@ubuntu-pc:~$ mkdir data2
testuser@ubuntu-pc:~$ ls
data data2 sensors.txt testfile.txt
- -p option can used to create nested directories with same command
- Example where data3, data4 and data5 directories are created (data5 is a subdirectory of data4 and data4 is a subdirectory of data3)
testuser@ubuntu-pc:~$ ls
data data2 sensors.txt testfile.txt
testuser@ubuntu-pc:~$ mkdir -p data3/data4/data5
testuser@ubuntu-pc:~$ ls data3/data4
data5
CP (Copy File or Directory)¶
- cp command is used for copying files and directories from one directory to another
- Source and destination directories are provided as arguments for the command
- Example: Let's copy sensors.txt file to data3 directory and give file a new name (sensors_new.txt)
testuser@ubuntu-pc:~$ cp sensors.txt data3/sensors_new.txt
testuser@ubuntu-pc:~$ ls data3
data4 sensors_new.txt
- When copying a directory -r (recursive) option must be used
- Example: Let's copy data4 directory from data3 directory into current working directory (.)
testuser@ubuntu-pc:~$ ls
data data2 data3 sensors.txt testfile.txt
testuser@ubuntu-pc:~$ cp -r data3/data4 .
testuser@ubuntu-pc:~$ ls
data data2 data3 data4 sensors.txt testfile.txt
MV (Rename and Move Files)¶
- Renaming and moving files and directories is done with mv command
- Similar to cp command, source and destination directories are provided as arguments
- Example: Let's move sensors.txt file into data4 directory and give destination file a new name (moved_sensors.txt)
testuser@ubuntu-pc:~$ ls
data data2 data3 data4 sensors.txt testfile.txt
testuser@ubuntu-pc:~$ mv sensors.txt data4/moved_sensors.txt
testuser@ubuntu-pc:~$ ls
data data2 data3 data4 testfile.txt
testuser@ubuntu-pc:~$ ls data4
data5 moved_sensors.txt
- Important: When copying source file to destination source will last. When moving source file to destination source file will be removed!
RM (Delete Files and Directories)¶
- rm command is used to delete files and directories
- One or more targets must be provided as arguments for the command
- Like with cp command, -r option must be used when deleting directories (recursive action)
- Example: Let's delete data4 directory including all its contents
testuser@ubuntu-pc:~$ ls
data data2 data3 data4 testfile.txt
testuser@ubuntu-pc:~$ rm -r data4
testuser@ubuntu-pc:~$ ls
data data2 data3 testfile.txt
Wildcards¶
- Wildcards are characters which can be used as filters for commands (cp, mv, rm, ls etc.)
- Wildcards:
- * equals zero or more characters
- ? equals any single character
- [list] equals any given character included inside the list
- Below are a few examples of wildcard use
- The directory contents are presented below
testuser@ubuntu-pc:~$ ls
sensor1.r sensor2.c sensor3.log sensor4.log server1.log server2.log
- Example: Let's list all files ending with .c string
testuser@ubuntu-pc:~$ ls *.c
sensor2.c
- Example 2: Let's list all sensor files ending with .log string
testuser@ubuntu-pc:~$ ls sensor?.log
sensor3.log sensor4.log
- Example 3: Let's list all sensor files ending with c or r characters
testuser@ubuntu-pc:~$ ls sensor?.[cr]
sensor1.r sensor2.c