×

Search anything:

Linux directories: pwd, ls, cd, mkdir, rmdir, cp, mv

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article we discuss Linux commands used to manage directories.

Table of contents.

  1. Introduction.
  2. pwd.
  3. ls.
  4. cd.
  5. mkdir.
  6. rmdir.
  7. mv.
  8. cp.
  9. Summary.
  10. References.

Introduction.

In this article we discuss Linux commands used to manage directories and perform operations such as listing directories, printing the current working directory, creating and removing a directory, copying a directory, moving a directory from its current location to another or renaming the directory.

pwd.

The pwd(print working directory) command is used to print out the current directory we are in.

The syntax is as follows,

pwd

Output.

/home/user

The output states that we are currently in the user directory which is the working directory.

ls.

The ls(list directory) command is used to list files and subdirectories within a directory.

The basic syntax is as follows,

ls

Commonly used options include,

  • ls -a, to print out all files including hidden files which are preceded with a period .

  • ls -l, to list files in a long list. Files are listed together with their permissions, ownership, time stamps.

  • ls -l --block-size=SIZE, to list files and directories while displaying their sizes.
    for example to list files and show sizes in megabytes we write,

ls -l --block-size=M

We could also use G for gigabytes, K for kilobytes, T for terabytes, P for petabytes and so on.

  • ls -d */, to list only subdirectories, this command will exclude files in its listing.

  • ls -g, to exclude owner information in the listing.

  • ls -lG, to exclude group information in the listing.

  • ls --color=COLOR, to add color, or remove color in the listing.

ls --color=never

To de-colorize the listing.

ls --color=auto

To color the listing.

  • ls -F, lists files and subdirectories with distinction, it shows differences between files, directories, executables.

  • ls -R, to list subdirectories recursively.

  • ls -Rl, list subdirectories recursively and in a long list.

cd.

The cd(change directory) command is used to change the current working directory. This command enables users to navigate through the system's directories.

The syntax is as follows,

cd [DIRNAME]
  • To change from the current directory to another directory we write,
cd /home/user/Desktop

We could also write,

cd ~/Desktop

since the ~ character represents the home directory.

  • To change directory using an absolute path, we write the entire path starting from root / directory.
    An example
cd /etc/network/

/etc/network/ is an absolute path.

  • To change a directory using a relative path, we write,
cd Desktop

Desktop is a relative path since it is relative to the current directory.

  • To change to the previous directory we use - character,
cd -

If we were in Documents directory and change to Downloads, executing the command will take us back to Documents, if we execute it again, we will go back to Downloads.

  • To change from the current directory to the parent directory of the working directory we write,
cd .
  • To change to another user's directory, we write,
cd ~username
  • We can also change into subdirectories as follows,
cd /home/user/Desktop/files/
  • We can also change directories as follows,
user:~$ cd Documents/Dir1/Dir2/Dir3

user:~/Documents/Dir1/Dir2/Dir3$ cd ..

user:~/Documents/Dir1/Dir2$ cd ..

user:~/Documents/Dir1$ cd ..

user:~/Documents$

mkdir.

The mkdir(make directory) command is used to create a new directory.

The syntax is as follows,

mkdir [DIRNAME]
  • To create a single directory we write,
mkdir newDir
  • To create multiple directories we write,
mkdir newDir1 newDir2 newDir2
  • To print output of the command we use the -v option,
$ mkdir newDir
mkdir: created directory 'newDir'
  • To create new subdirectories inside a newly created directory or an existing one we use the -p option. If the directory exists, no error will be printed, otherwise new subdirectories will be created inside the directory.
mkdir -p newDir/dir1 newDir/dir2
  • Create a directory while specifying permissions we use the -m, --mode=MODE option,
    An example
    To create anew directory with full access(read, write, execute) we write,
mkdir -m 777 newDir

rmdir.

The rmdir(remove directory) command is used to delete an empty directory.
The syntax is as follows,

rmdir [DIRNAME]

To remove a non-empty directory, that is a directory with files and subdirectories we use the rm -r dirname command.

mv.

The mv(move) command is used to move files and directories in Linux, it can also be used to rename files and directories.
The syntax is as follows,

mv [OLD] [NEW]
  • To change the name of NewDir to ChangedDir we write,
mv newDir ChangedDir
  • To move a directory/file we can write,
mv newDir ~/Documents/Dirs

This command will remove newDir from the current directory and move it to ~/Documents/Dirs.

cp.

The cp(copy) command is used to copy files or directories.

cp -r newDir ~/Documents/Dirs

The command will copy newDir directory into ~/Documents/Dirs. Note that newDir will not be removed from where it is. It is only copied.
We use the -r to recursively copy files and subdirectories.

To copy files we write,

cp file.txt ~/DocumentsDirs

Summary.

In summary, pwd prints the working directory, ls command lists files and subdirectories in the current working directory, cd is used to change directories, mkdir creates directories while rmdir removes empty ones, cp command copies files and directories locally and mv command can be used to either move or rename a directory

References.

  1. Execute the command man command or command --help for the manual or help.
Linux directories: pwd, ls, cd, mkdir, rmdir, cp, mv
Share this