Linux Basic – Linux and Unix Commands for File and Directory Manipulation
Many users have started buying Linux laptops from the past few years, which indicates that the Linux desktop operating system is gaining more users.
What prevents users from buying a Linux laptop? I believe, they may think, it is very difficult to use.
In my personal experience, then, your opinion is not correct because Linux desktop operating systems are better than Windows.
I’ve been using the Linux laptop since 2014 and never want to go to Windows.
Are you planning to move from Windows to Linux, but don’t know where to start?
Don’t worry, you’re on the right page and the right form can start here. We are here to help you understand the basic Linux commands for file and directory management.
For Linux beginners I have deeply prepared the file and directory base commands. This training may help them learn easier.
In this article, I have added more than 10 Linux commands, which are related to file and directory management.
|ls||To list the contents of a directory|
|cp||To copy files and directories from one location to another.|
|mv||To move files and directories from one location to another and rename a file or directory.|
|rm||To delete files or directories.|
|cd||To change the current working directory.|
|pwd||To print Present Working Directory|
|ln||To create a symbolic link file or directory.|
|mkdir||To create a new directory|
|rmdir||To delete only an empty directory.|
|unlink||To delete a regular file and symlink file.|
1) What is ls Command
The ls command displays the standard output of the current directory contents. If you specify a specific directory or parameter, the output will be displayed based on that.
It supports many options and you can use that option to customize the output to suit your needs. ls command is the heart for Linux user and they cannot live a day without using it.
Common Syntax for ls Command
Syntax: ls [Option] [Directory_Name]
How to Use ls Command on Linux
# ls -lh total 24K -rwxr-xr-x 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh* -rwxr-xr-x 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh* -rwxr-xr-x 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh* -rwxr-xr-x 1 daygeek daygeek 98 Aug 19 00:48 passwd-up1.sh* -rwxr-xr-x 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh* -rwxr-xr-x 1 daygeek daygeek 18 Aug 19 12:33 test.txt*
Alternatively, you can use the exa command, which is a colorful alternative to the ls command. It is written in rusty language and looks small, fast, portable and modern.
2) What is cp (copy) Command
The cp (copy) command is one of the basic Linux commands for copying files and directories from one location to another.
When copying files from source to destination, the source file name does not change, but we can change the target file name if we need to.
Common Syntax for cp Command
Syntax: cp [options] [Source] [Destination]
How to Use cp (copy) Command on Linux
For demonstration purposes, we will copy the
tanisha.txt.gz file from the “/home/daygeek/shell-script/backup/” directory to the “/home/daygeek/shell-script/backup/old” directory. See the output below.
$ cp /home/daygeek/shell-script/backup/tanisha.txt.gz /home/daygeek/shell-script/backup/old $ ls -lh /home/daygeek/shell-script/backup/old total 28K -rwxr-xr-x 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh* -rwxr-xr-x 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh* -rwxr-xr-x 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh* -rwxr-xr-x 1 daygeek daygeek 98 Aug 19 00:48 passwd-up1.sh* -rwxr-xr-x 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh* -rw-r--r-- 1 daygeek daygeek 149 Jan 27 20:26 tanisha.txt.gz -rwxr-xr-x 1 daygeek daygeek 18 Aug 19 12:33 test.txt*
2a) What is scp Command
The scp command copies files between hosts on a network. It uses ssh for data transfer, uses the same authentication and provides the same protection as ssh.
If you want to try any other alternatives, go to the following URLs.
3) What is mv (move) Command
mv (move) is a Linux command that moves one or more files or directories from one location to another. It can also be used to rename a file or directory.
Common Syntax for mv (move) Command
Syntax: mv [options] [Current_Name] [New_Name]
How to Use mv (move) Command on Linux
To test this, we will copy the
test.txt file from the “/home/daygeek/shell-script/backup/” directory to the “/home/daygeek/shell-script/backup/old” directory with the new name
magesh.txt. See the output below.
$ ls -lh /home/daygeek/shell-script/backup/*.txt -rw-r--r-- 1 daygeek daygeek 96 Jan 4 21:57 /home/daygeek/shell-script/backup/2daygeek.txt -rw-r--r-- 1 daygeek daygeek 94 Jan 4 21:56 /home/daygeek/shell-script/backup/2gtest.txt -rw-r--r-- 1 daygeek daygeek 18 Aug 19 12:33 /home/daygeek/shell-script/backup/test.txt
It has been moved successfully and you can find it in the target directory.
$ mv /home/daygeek/shell-script/backup/test.txt /home/daygeek/shell-script/backup/old/magesh.txt $ ls -lh /home/daygeek/shell-script/backup/old/*.txt -rwxr-xr-x 1 daygeek daygeek 18 Aug 19 12:33 /home/daygeek/shell-script/backup/old/magesh.txt*
4) What is rm Command
The rm command is used to remove files, directories, device nodes, symbolic links, etc. This is a very dangerous command and be aware every time you use the rm command.
You must use the
-r option to delete files or directories recursively.
Common Syntax for rm (remove) Command
Syntax: rm [options] [Files or Directories]
How to Use rm (remove) Command on Linux
use the rm command below to remove any given file.
$ rm magesh.txt
For multiple files.
$ rm magesh.txt thanu.txt
To delete directory recursively.
$ rm -Rf /home/magesh
5) What is cd Command
The cd command refers to the change directory, which is used to change the current working directory. If the directory is provided, it will go to that directory.
If no parameters are provided, it will go to the current user home directory. If you are given a directory name starting with the
~ (tilde) symbol, your shell will attempt to change the directory without the need of a cd.
Common Syntax for cd Command
Syntax: cd [Directory to be Navigate]
How to Use cd Command on Linux
To go to the given directory.
$ cd /home/daygeek/shell-script/backup/old $ pwd /home/daygeek/shell-script/backup/old
To go back to the user home direcoty.
$ pwd /home/daygeek/shell-script/backup/old $ cd $ pwd /home/daygeek
To go to the specific directory with out cd command, use the
~ (tilde) symbol.
$ pwd /home/daygeek $ ~/shell-script/backup/old $ pwd /home/daygeek/shell-script/backup/old
We have written some articles on this topic, which helps to speed up a directory navigation in Linux. To do so, go to the following articles.
- Pushd & Popd : Easy way to navigate between directories faster in Linux
- Quickly navigate to specific parent directory using bd command in Linux
- How to navigate inside a Directory/Folder without using CD command in Linux
- UP Shell Script – To Navigate specific parent directory in Linux
6) What is pwd Command
The pwd command stands for Present Working Directory or print current working directory, which is used to output the path of the current working directory.
Common Syntax for pwd Command
Syntax: pwd [Options]
How to Use pwd Command on Linux
Run the pwd command on your terminal to get the current working directory.
$ pwd /home/daygeek/shell-script/backup/old $ ls -lh total 28K -rwxr-xr-x 1 daygeek daygeek 18 Aug 19 12:33 magesh.txt* -rwxr-xr-x 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh* -rwxr-xr-x 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh* -rwxr-xr-x 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh* -rwxr-xr-x 1 daygeek daygeek 98 Aug 19 00:48 passwd-up1.sh* -rwxr-xr-x 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh* -rw-r--r-- 1 daygeek daygeek 149 Jan 27 20:26 tanisha.txt.gz
7) What is mkdir Command
The mkdir command is a basic Linux / UNIX command that can be used to create a new directory, if it is not already present.
Common Syntax for mkdir Command
Syntax: mkdir [Directory_Name]
How to Use mkdir Command on Linux
The mkdir command allows users to create a new directory.
$ mkdir /home/daygeek/ansible
8) What is rmdir Command
You can easily remove empty directories using the rmdir command.
Common Syntax for rmdir Command
Syntax: rmdir [options] [Empty_Directory_Name]
Run the below command to delete/remove the empty directory named “2g-test”.
$ rmdir /home/magesh/2g-test
9) What is ln Command
The ln command is used to create a link file or directory on Linux.
A symbolic link, also known as a symlink or soft link, is a special type of file that points to another file or directory in Linux.
It’s similar to a shortcut in Windows.
It can point to a file or a directory on the same or a different filesystem or partition.
Common Syntax for ln Command
Syntax: ln -s [Source_File_Name or Directory_Name] [Symbolic_Link_File_Name]
How to Use ln Command on Linux
You can use the following command to create a soft link. Make sure the target directory does not contain a link file name. If it is, you will receive the error message below.
$ ln -s service11.sh /home/daygeek/shell-script/backup/old/daygeek.txt $ ls -l daygeek.txt lrwxrwxrwx 1 daygeek daygeek 12 Jan 28 11:39 daygeek.txt -> service11.sh
$ ln -s service11.sh /home/daygeek/shell-script/backup/old/magesh.txt ln: failed to create symbolic link '/home/daygeek/shell-script/backup/old/magesh.txt': File exists
10) What is unlink Command
The unlink command deletes a given regular file and symlink file. It accepts only a single file at a time.
Common Syntax for unlink Command
Syntax: unlink [Regular_File_Name or Symlink_File_Name]
Run the below command to unlink the given symbolic link file.
$ unlink daygeek.txt