This Linux How To will cover Deleting a File, Deleting a Directory, Making a File, and Making a Directory. Although this is not meant to be an exhaustive list of file and directory management it’s a quick intro for someone just starting their Linux journey.
Table of Contents
- Linux How To: Make a Directory
- Linux How To: Delete a Directory
- Linux How To: Make a File
- Linux How To: Delete a File
- Linux How To: Do a file rename
Introduction
Managing files and directories is an essential part of maintaining and using any Unix/Linux system.
The Linux file system works similarly to Windows and MacOS if you are familiar with those operating systems. Although Linux is much easier and faster to get around if you get comfortable with the Command Line Interface or CLI.
Directory Management
Linux How To: Make a Directory
There are two primary ways of making a directory. There is the visual way using a Graphical User Interface (GUI) and there is the CLI. I will primarily go over using the CLI.
Making a directory involves the use of the mkdir
command, which stands for Make Directory but made into a short command.
mkdir
has several arguments that can be passed in but only the name of the directory is mandatory.
The base command looks like the following:
exploit@exploitBrokers:~$ mkdir sampleDirectory
If you entered a command similar to the above you would now have a directory named sampleDirectory
available. You can view this by entering the list contents command ls
.
You should have similar output when you enter the ls
command
exploit@exploitBrokers:~$ ls
sampleDirectory
If you enter the list command with the arguments -l
which indicate that ls
should use the long listing format then you will get an output similar to the following
exploit@exploitBrokers:~$ ls -l
drwxr-xr-x 2 exploitUser exploitGroup 4096 Jun 1 23:30 sampleDirectory
The following line is broken down as follows:
d
is the file type, this indicates it is a directory.
rwx
each three characters indicate whether the owner/group/others have permissions and what permissions they have. Our current directory allows the owner to r
– read, w
– write, and x
– execute the directory.
r-x
allows the group to read and execute but not write.
r-x
allows all others to read and execute but nor write.
exploitUser
is the owner of the directory.
exploitGroup
is the group that the directory belongs to.
The rest is the size, date created, and name of the directory or file.
Linux How To: Delete a Directory
Deleting a directory involves a normal delete/remove command rm
and appending an optional flag.
Although the flag is optional for the remove command when files are involved things differ slightly for directories. Attempting to delete a directory without an additional flag results in the following output.
exploit@exploitBrokers:~$ rm ./sampleDirectory/
rm: cannot remove './sampleDirectory/': Is a directory
This is easily fixed by appending a recurse flag -r
. The following is an example of a complete command.
exploit@exploitBrokers:~$ rm -r ./sampleDirectory/
This results in the directory being deleted and all files and directories inside being deleted as well.
File Management
Linux How To: Make a File
Now that you can make and delete directories you will need to learn how to make and delete files. In Linux there are a multitude of file types in Linux but the majority of files are recognized simply as regular files. JPEG, PNG, MOV, PDFs, and etc are considered regular files. We will discuss other non-regular file types in another article.
Making a file can depend on the file contents and the application that will use it. Although a PDF and JPEG are both regular files the contents will differ. We will make a simple text file with a greeting.
exploit@exploitBrokers:~$ echo "hello" > someFile.txt
Here we are using the echo
command to print our “hello” text out and then using the >
pipe into command to put it into someFile.txt
. It’s nice to note we don’t need the .txt
extension but it’s nice to have to keep things easy to view. You can easily have a JPEG with a .txt file at the end because Linux tries to be smart about files and can usually determine the file type based off the content and not just the extension.
If we print out the content of the file using the cat
command we would get the following:
exploit@exploitBrokers:~$ cat someFIle.txt
hello
It’s that simple to create a file. You can also use the touch
command to make an empty file that you can then open up in a text editor to add content to and the possibility for file creation is endless. Here is an example of the touch
command before we move on.
exploit@exploitBrokers:~$ touch someFile.txt
Linux How To: Delete a File
Deleting a file is generally just as easy if not easier than making a file. Deleting a file uses the same rm
command as deleting a directory but does not require the recursive argument.
If we wanted to delete the file we created earlier we would simply use the following command:
exploit@exploitBrokers:~$ rm someFile.txt
If an error occurs then either run the command as a more privileged user or you can use the force -f
flag. Note additional flags are advisable if you have more experience or are willing to make mistakes and learn from your mistakes while learning Linux.
Linux How To: Do a File Rename
An easy way to do a file rename in linux is to use the mv
command. The command syntax is as follow
exploit@exploitBrokers: ~$ mv oldFileName.txt newFileName.txt
The above command is the quickest way to rename a file on linux.
Linux How To: Conclusion
This was a short introduction to making and deleting files and directories. A helpful hint is to use the --help
flag with commands or to reference the man
command if available. These are helpful as they usually provide extra information for users to read and try to figure out what to do next.
Leave a Reply