Linux Tutorial - Working With Directories
Information is usually stored in directories (folders) to
organize it. Basic Linux commands are covered here.
1. Listing Directories
As we did with files, we'll use the ls command to see
which exist at our current location in the directory tree.
machine:~$ ls -1
mydata.zip.backup
mydirectory/
machine:~$ file mydirectory
mydirectory: directory
|
|
In addition to listing entries, we also used file to verify
that mydirectory was indeed a directory. This step is
redundant as ls added a / after mydirectory.
2. Moving Around
To change your current location from one directory to another, use the cd Linux command.
machine:~$ cd mydirectory
|
|
This changed our current location to mydirectory.
3. Where Am I?
To view your current location in the directory tree, use pwd.
machine:~$ pwd
/home/username/mydirectory
|
|
This prints the full name/path of the working/current directory.
Let's move back to where we started.
machine:~$ cd /home/username/
|
|
A quicker way would have been to move one step up in the directory
tree, which is done as follows.
machine:~$ cd
machine:~$ pwd
/home/username/
|
|
Without any location specified, cd will take you back to your
home directory.
4. Creating New Directories
Let's make a directory called personal, using mkdir.
machine:~$ mkdir personal
machine1:~/test$ ls -1
mydata.zip.backup
mydirectory/
personal/
|
|
The ls Linux command was also used to verify success.
5. Removing Directories
Now let's remove the one we just created.
machine:~$ rm personal
rm: personal: is a directory
|
|
The rm command will not let us remove a directory,
unless we specify '-r' to remove it recursively.
machine:~$ rm personal -r
machine1:~/test$ ls -1
mydata.zip.backup
mydirectory/
|
|
Now it was removed, which we verified by using ls.
Please note that the above rm command will remove the
directory and any files in it. Those files can not be recovered.
Do this only if you are certain you no longer need the directory
nor the files in it.
For more information on any of the Linux commands covered here,
see the more help tutorial section.