Main menu

How to manage files and directories on GNU/Linux?

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
 
Last modified onThursday, 22 September 2016 20:48
Rate this item
(3 votes)
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

5 comments

  • Idelkys
    Idelkys Monday, 25 November 2013 15:50 Comment Link

    I find this post very useful! We often don't know the shortcuts that simplify our interaction with the command line in Unix... here we can find many utilities to do that easily.... the "man man" command is very interesting! Thx

  • Sequeira
    Sequeira Monday, 25 November 2013 20:21 Comment Link

    Hi Idelkys, thank you for your comment. It is good to know that it is useful for someone.

  • pletcherhwb
    pletcherhwb Tuesday, 30 December 2014 06:33 Comment Link

    I will keep a constant attention to your articles. Really good stuff!

  • Miguel Abad
    Miguel Abad Friday, 04 March 2016 18:51 Comment Link

    good stuff, but i don't use Linux. I prefer Mac.
    Good job!

  • Francis Cockerill
    Francis Cockerill Thursday, 17 March 2016 17:18 Comment Link

    Try to use https://www.bing.com/ for search results! Fresh your life!)

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

back to top

Facebook TwitterRSS