Unlocking the CLI: File and Directory Essentials for Every User
Anyone dealing with a computer, whether they are an experienced IT expert, a software developer, or just an average user, has to be familiar with file and directory commands. You can effectively navigate, work with, and manage files and directories with these commands in a command-line environment. In this article, we'll look at several crucial file and directory commands that will teach you how to master the command line.
1. pwd (Print Working Directory)
pwd command is used to know your current directory. This command shows the full path to your current location in the file system.
2. cd (Change Directory)
Use cd
followed by a directory path to change your current location. For instance, to move to the "Downloads" directory, type cd Downloads
Here we can see we moved from the Home directory to the Downloads directory. And again using cd .. we are back to the Home directory again.
3. ls (List)
The ls
command lists the contents of the current directory. Adding options like -l
a detailed list or -a
to show hidden files can provide more information. For more options, we can navigate the manual page of the list using the command man ls.
We can see all the files in detail along with hidden files represented by . and .. .
Manipulating Files and Directories
touch
The touch
the command creates an empty file. For example, touch demo.txt
will create a file named "demo.txt."
mkdir (Make Directory)
To create a new directory, use mkdir
. For instance, mkdir demofolder
creates a directory named "demofolder."
cp (Copy)
cp
allows you to copy files or directories. Use it with the source and destination paths to specify what to copy and where to copy it.
cp source_file destination_path
Here we copied the demo.txt to the demofolder, we can see the demo.txt file in the demofolder.
mv (Move/Rename)
The mv
command can be used to move files or directories to a different location or rename them.
mv source_Files destination_path
Using the mv command, we moved all the files and directories i.e. demo.txt, movefile.txt, movefolder to the demofolder.
rm (Remove)
To delete files or directories, use rm
. Be cautious when using it, as deleted items are not typically recoverable. For files, we can use rm and for directories, we can use rmdir to delete them.
options used with the rm command:
-r: Allows the removal of directories and their contents recursively.
-f: delete the files forcefully and do not prompt for confirmation.
-v: Provides detailed information about the removal process.
-d: Removes empty directories.
First, we deleted the movefolder using rmdir command and then we deleted the demo.txt file using the rm command. When we tried to delete the demofolder we got the error: "Directory not empty" since movefile.txt was still there. So using the -r (recursively) option, we deleted the demofolder folder. Since we deleted all the contents of the TEST directory, the ls command gives no output as nothing is there to show.
Tab Completion
Save time by using tab completion. Start typing a file or directory name, then press the Tab key to auto-complete it. If there are multiple matches, it will display options.
On pressing the Tab key after typing Do we see the multiple files that are matching Do.
Being proficient with file and directory commands can boost your productivity and give you more control over the file system on your computer. In a command-line environment, these commands provide you the freedom to swiftly navigate, manipulate, and manage files and directories. You'll improve your confidence and skill in using these crucial commands with practice. So, start experimenting and opening your terminal to unleash the command line's potential!
Happy Learning!!
Subscribe to my newsletter
Read articles from Subash Neupane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Subash Neupane
Subash Neupane
Computer Science graduate