Handling the filesystem in Kali
I could write a whole book for this section, but I will do my best to show you the commands that you're going to need in your arsenal as a penetration tester.
Before I start listing all of the commands, I would like to share a unique command-line utility that you'll need to master before starting with any of the utilities in this chapter. You can probably guess it; it's called the Help switch! This option will give you a handful of information regarding the command that you are going to execute.
For example, if you want to list the contents of a directory and you're not sure of the options for the command, all you need to do is append the --help switch, and you can visualize all of the possible functionalities of the command:
Are you ready? This is going to be a long list (a sort of cheat sheet). Let's start:
- To list the directory and files, use the following:
ls
- To list hidden items (-a) in a formatted way (-l), use the following:
ls -la
- To list files and directories in a human-readable form, use the following:
ls -lh
- To change the current directory to a new one, use the following:
cd [directory path]
- To print the working directory, use the following:
pwd
- To make a new directory, use the following:
mkdir [path/directory name]
- To remove (delete) a file, use the following:
rm [path/file name]
- To delete a directory, use the following:
rm -r [path/directory name]
- To copy a file to a new location, use the following:
cp [path1/file name] [path2/filename]
- To copy a directory to a new location, use the following:
cp -r [path/directory name]
- To move/rename a file or a directory, use the following:
mv [path1/file name] [path2/file name]
mv [path1/directory name] [path2/directory name]
- To create an empty file, use the following:
touch [path/new file name]
- To display the contents of a file, use the following:
cat [path/file name]
more [path/file name]
#list the first 10 lines
head [path/file name]
#list the last 10 lines
tail [path/file name]
- To open a text file for editing, use the following:
gedit [path/file name]
#Terminal window text editor (Some people use the Vim editor but that's not my choice)
nano [path/file name]
- To find files on your Kali box, use the following:
locate [file name]
find [Path where to start the search] -name [file name patterns]
- The command that will list the drives (partitions) on the system (for example, /dev/sda1 or /dev/sda2) is as follows:
fdisk -l
- To mount an unmounted partition, use the following:
mount [path source] [path destination]
#Example mounting a hidden windows drive that is already installed on the same machine
#Already executed $fdisk -l and saw a drive /dev/sda2
mount /dev/sda2 /mnt/windowsmount
- To check the type of a file, use the following:
file [path/file name]
- To add the execute permission to a file (sometimes you will need it because by default you cannot execute it), use the following:
chmod +x [path/file name]
- To redirect the output of the Terminal window to a file, use the following:
[command] > [path/filename]
#Example to save the ls command output to a file called output.txt
ls -lh > /root/temp/output.txt
- To filter text in a text file or in a command Terminal output, use the following:
grep [text to filter]
The grep command is most frequently used with the Pipe symbol, |, to filter text coming out from the Terminal window. For example, to filter the word password in a text file called config.txt, you would use the following command:
cat config.txt | grep password