This article gives a brief overview of the most common commands for working with files and directories under the Linux system. The Linux edition used during this tutorial is Kali Linux. However, any Linux distribution can be used to practice the exercises presented in this post.
Working with files and directories is an essential skill that any Linux or computer security professional must master. For instance, most security programs, especially those used in penetration testing and digital forensics, are working under Linux OS, and many of these tools do not have a Windows edition. Knowing how to navigate a Linux directory and create files is essential for working with such tools. This article will cover the most-used commands and give examples of how to use each one.
The following are the most used commands to work with files and directories in Linux:
- pwd: The command displays your current working directory. For example, in Figure 1, I'm now in the Myfolder directory, located inside the Desktop directory.
- cd: We use this command to change the current working directory. For example, in Figure 2, I'm moving to the root directory.
- ls: For listing the contents of the current directory. In Figure 3, I'm listing the contents of the Desktop directory.
We can add the la command to the ls command switch to display all hidden files within a directory. See Figure 4.
- mkdir: This command will create a new directory if it does not already exist.
To create a nested directory (subdirectory) within a new directory, use the –p command.
- To create two directories at the same time, use the following command: mkdir dir1 dir2
- To move a directory from one place to another, use the following command. mv dir1 new_dir
- rmdir: This command will remove the existing directory.
- To copy a directory, use the CP command (cp -a sourceDirectory DestinationDirectory). See Figure 7.
Working with files
- To create a new file (for example, myfile.txt), use the following command in Figure 8:
- To delete a file name, use the rm command. See Figure 9 for an example:
- To copy the content of a file into another, use the following command (see Figure 10).
Symbols for Directories in Kali Linux:
Sign '~' means Home Directory. Sign '/' means File System Directory For 'desktop folder' in Kali Linux, we write it as 'Desktop' (notice capital D here)
Executing the Commands practically:
root@kali:# Means we're in the home folder Write 'cd /' root@kali:# cd / Means we changed directory from ~ to / Write 'ls' root@kali:/# ls Means it will list/show the content of / directory Make a new folder 'test' on the desktop. For that, execute the following: root@kali:/# cd ~ root@kali:# cd Desktop root@kali:/Desktop# mkdir test
A new folder, 'test' will be created on the desktop now. To change directory to 'test,' use 'cd test,' and to list the contents, use 'ls':
root@kali:/Desktop# cd test root@kali:/Desktop/test# ls
To display the working directory, type 'pwd': root@kali:~/Desktop/test# pwd
It will o/p 'test' in the next line because we're in a 'test' folder, and pwd prints the working directory. Now, to remove the directory 'test,' execute the following: root@kali:~/Desktop/test# cd Desktop Means we are changing to Desktop (getting out from 'test)
root@kali:~/Desktop# rmdir test Means we are deleting 'test' directory
Well, that's the basics of working with directories in Kali Linux. As we move along, we'll encounter many other useful commands for working with the directory. If you're confused about how to execute these commands, please watch my video tutorial demo/lab here: https://youtu.be/svXodC_eJls Feedback is sweet, so please provide it via comment. :)