Print this page

How to manage files and directories on GNU/Linux?

  • font size
Rate this item
(4 votes)

Before starting with the use of some Linux commands, I want to remind the importance of reviewing the reference manual for each command, because this is the place where you can find all the possible options. The reference manual can be displayed on a terminal using the "man" command, for example, if we see the manual of the "man" command in a terminal, we should write "man man" and we would see something like this:

Figure # 1: Linux terminal displaying the manual of the man command.

One of the most used command is "cd", it is used to access a directory, here are some examples:

cd                  # go to the personal directory
cd ..               # back one level
cd ../..            # back two levels
cd -                # go to previous directory
cd /home            # go to /home directory
cd ~user1           # go to the directory user1

If you want to know the current route:

pwd                 # display the current directory path

Once inside a directory, you might want to see the content, hidden files, permissions or details of each files or subdirectories, in this case "ls" is the solution:

ls                  # list the contents of a directory
ls -F               # distinguishing the directories with a slash
ls -l               # showing the details
ls -lh              # showing the details(in a size format of K, M)
ls -a               # including hidden files
ls *[0-9]           # list the files and folders that contain numbers
tree                # show files and folders in a tree starting by the root

To create directories we use "mkdir" like this:

mkdir dir1                    # create a directory called dir1
mkdir dir1 dir2               # create two directories at once
mkdir -p /dir1/dir2/dir3      # create a directory structure, if not there

To delete files and directories:

rm file1                      # delete the file file1
rm -f file1                   # remove file1 in forced mode
rm -r dir1                    # recursively delete the directory dir1 with all contents
rm -rf dir1 dir2              # remove two directories with their contents recursively and forced

To move (rename), copy files and folders we need two different commands:

mv old_dir new_dir                 # rename or move a file or directory
cp file1 /path/to/destiny/         # copy a file to destiny
cp file1 file2 destiny/            # copy two files simultaneously
cp file1 file2                     # copy file1 in file2
cp -r dir1 destiny/                # copy a directory

To create symbolic links (shortcuts):

ln -s file link                    # create a symbolic link to the file or directory

To find files there are several options one of the most common is using the command "find", though some users find it annoying as it may wait too much time in large systems, so they prefer to use "locate", for this command to work properly, the database must be updated by means of "updatedb" command.

find / -name file1                              # search file and directory from the system root
find / -user user1                              # belonging to user1
find /home/user1 -name \*.bin                   # with .bin extention in the directory / home/user1
find /usr/bin -type f -atime +100               # unused binary files in the last 100 days
find /usr/bin -type f -mtime -10                # files created or changed within the last 10 days
locate \*.ps                                    # find files with .ps extentions
whereis file                                    # show the location of a binary file, help, or source
which command                                   # show the full path to a command

 

References

  1. Red Hat Enterprise Linux: Manual de referencia
 
17296 Last modified on Thursday, 22 September 2016 19:48
Luis Sequeira

Luis Sequeira is an IT professional with experience in cloud environments, quality of service and network traffic analysis, who loves looking for solutions to engineering challenges, share knowledge. At work, the main challenge is to integrate different network and software technologies to provide solution in a wide range of areas, e.g., virtual network functions, machine learning, autonomous driving, robotics and augmented reality.

Website: https://www.luissequeira.com

Latest from Luis Sequeira

Related items

5 comments