LPIC 101 – Managing Files and Directories on Linux Terminal

Linux systems treats everything as files, having knowledge on how to manipulate through these files is very important in our day to day work as System Administrators. A file is an entity that stores data and programs. It consists of content and meta data (file size, owner, creation date, permissions). Files are organized in directories. A directory is a file that stores other files. Skilled Linux user or System Administrator you are required to navigate through the file system, copy files from one location to another and delete files.

There are different types of file.

  • Regular files: which store data and programs.
  • Directories: which contain other files.
  • Special files: which are used for input and output.

Through this guide, we will learn how to Manage Files and Directories on Linux Terminal.

Copying Files and Directories

Copying Files

To copy a file or directory locally, use the cp utility. To issue this command, you use cp along with any needed options or arguments.

Syntax:

cp [OPTION]… SOURCE DEST

Let’s copy file listing.txt using cp command to subdirectory MyFiles:

$ cp -v listings.txt MyFiles
'listings.txt' -> 'MyFiles/listings.txt'

Using -v (--verbose) will provide detailed command action information as command executes.

Copying Files and Directories Recursively

cp -r (or -R or --recursive) allows you to copy a directory together with its all subdirectories and files.

Let’s copy Documents directory to Desktop directory recursively:

cp -rv ~/Documents ~/Desktop

The recursive copy option is one of the few command options that can be uppercase, -R, or lowercase, -r.

Moving/Renaming Files and Directories

Moving Files

To move or rename a file or directory locally, you use a single command: the mv command.

Syntax:

mv [OPTION]… SOURCE DEST

Example:

$ mv -v listings.txt Dir1
renamed 'listings.txt' -> 'Dir1/listings.txt'

Renaming Files

Renaming a file using the mv command, list the contents of our current directory using ls -lh command;

$ ls -lh
total 8.0K
-rw-rw-r-- 1 frank frank    0 Apr 13 18:21 classified.txt
drwxrwxr-x 2 frank frank 4.0K Apr 13 18:17 Dir1
-rw-rw-r-- 1 frank frank    0 Apr 13 18:26 listings.txt
-rw-rw-r-- 1 frank frank    0 Apr 13 18:23 Nextlist.txt
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:48 Test1

Let’s rename our file listing.txt to anotherlist.txt:

$ mv -v listings.txt anotherlist.txt
renamed 'listings.txt' -> 'anotherlist.txt'

Let’s now check if the file have been renamed using ls -lh command:

$ ls -lh
total 8.0K
-rw-rw-r-- 1 frank frank    0 Apr 13 18:26 anotherlist.txt
-rw-rw-r-- 1 frank frank    0 Apr 13 18:21 classified.txt
drwxrwxr-x 2 frank frank 4.0K Apr 13 18:17 Dir1
-rw-rw-r-- 1 frank frank    0 Apr 13 18:23 Nextlist.txt
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:48 Test1

In the above output, listing.txt has been renamed to anotherlist.txt.

Moving and renaming a file using the mv command

using ls -lh Dir1/ command to check the contents of Dir1 directory.

$ ls -lh Dir1/
total 0
-rw-rw-r-- 1 frank frank 0 Apr 13 18:16 listings.txt
-rw-rw-r-- 1 frank frank 0 Apr 14 10:29 troubleshoot.txt

using ls -lh Contents/ command to check the contents of Contents directory.

$ ls -lh Contents
total 0
-rw-rw-r-- 1 frank frank 0 Apr 14 10:28 file1.txt
-rw-rw-r-- 1 frank frank 0 Apr 14 10:28 myfile.txt

We can move and rename troubleshoot.txt in Dir1 directory to results.txt in Contents directory.

 $ mv Dir1/troubleshoot.txt Contents/results.txt

using ls -lh Dir1/ to display the contents of Dir1 directory.

$ ls -lh Dir1/
total 0
-rw-rw-r-- 1 frank frank 0 Apr 13 18:16 listings.txt

using ls -lh Contents/ to display the contents of Contents directory.

$ ls -lh Contents/
total 0
-rw-rw-r-- 1 frank frank 0 Apr 14 10:28 file1.txt
-rw-rw-r-- 1 frank frank 0 Apr 14 10:28 myfile.txt
-rw-rw-r-- 1 frank frank 0 Apr 14 10:29 results.txt

In the above output, the file troubleshoot.txt is located in the Dir1 directory. Employing a single mv command, it is moved to the Contents directory and renamed to results.txt at the same time.

Renaming Directories

When renaming an entire directory, there are no additional required command options. Just issue the mv command as you would for renaming a file.

Using ls -lh command let’s our available directories:

$ ls -lh
total 8.0K
-rw-rw-r-- 1 frank frank    0 Apr 13 18:26 anotherlist.txt
-rw-rw-r-- 1 frank frank    0 Apr 13 18:21 classified.txt
drwxrwxr-x 2 frank frank 4.0K Apr 13 18:17 Dir1
-rw-rw-r-- 1 frank frank    0 Apr 13 18:23 Nextlist.txt
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:48 Test1

Here we are going to rename Test1 directory to Contents.

$ mv -iv Test1 Contents
renamed 'Test1' -> 'Contents'

Issue ls -lh command to check the directory is renamed:

$ ls -lh
total 8.0K
-rw-rw-r-- 1 frank frank    0 Apr 13 18:26 anotherlist.txt
-rw-rw-r-- 1 frank frank    0 Apr 13 18:21 classified.txt
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:48 Contents
drwxrwxr-x 2 frank frank 4.0K Apr 13 18:17 Dir1
-rw-rw-r-- 1 frank frank    0 Apr 13 18:23 Nextlist.txt

The above output shows that the Test1 directory has been renamed to Contents. employing the -i option to avoid renaming a file to a preexisting file.

Creating and Deleting Directories

Creating Empty Directories

To create a directory, we use mkdir command. Creating directories is critical to organizing your files and folders. Files may be grouped together in a logical way by keeping them inside a directory.

Syntax:

mkdir OPTIONS DIRECTORY_NAME

Where DIRECTORY_NAME is the name of the directory to be created. Any number of directories can be created simultaneously:

Let’s create an empty directory Test:

mkdir Test

Check if the directory Test have been created using ls -lh command.

$ ls -lh
total 4.0K
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:37 Test

Creating more than one directory:

mkdir Test1 Test2 Test3

Employ ls -lh command to check created directories:

$ ls -lh
total 12K
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:48 Test1
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:48 Test2
drwxrwxr-x 2 frank frank 4.0K Apr 13 11:48 Test3

Creating Directories with Subdirectories

To create a directory together with its subdirectories use the option -p (“parents”):

$ mkdir -pv Test/Dir1
mkdir: created directory 'Test'
mkdir: created directory 'Test/Dir1'

In the above command, -v option print a message for each created directory, Test directory has been created together with its subdirectory Dir1.

Deleting Empty Directories

To delete an empty directory, we use rmdir command. Use together with -v option in order to print message for deleted directory.

Let’s delete an empty directory Test2:

$ rmdir -v Test2
rmdir: removing directory, 'Test2'

You can also delete an empty directory with rm command, add the -d (or –dir) switch in order to delete an empty directory with rm command.

$ rm -di Test3
rm: remove directory 'Test3'? y

Using -i (or –interactive) option to ensure that you are not deleting the wrong file, accept it using y key.

Deleting Directories with Subdirectories

The -p (or –parents) switch is required along with providing the entire directory tree name as an argument.

Let’s delete Test directory together with its subdirectory Dir1;

$ rmdir -pv Test/Dir1
rmdir: removing directory, 'Test/Dir1'
rmdir: removing directory, 'Test'

Deleting Directories Recursively

rm -r will remove a directory and all its contents (subdirectories and files).

Delete Test directory with its files and subdirectories;

$ rm -rv Test
removed directory 'Test/Dir1'
removed 'Test/song1.mp3'
removed 'Test/myfile.txt'
removed directory 'Test'

Determining File’s Type

On Linux, everything is a file. Well, if you aren’t sure what type of file it is, that can be tricky. To quickly determine a file’s type, use the file command.

Using the file command.

$ file vagrant_2.2.14_x86_64.deb
vagrant_2.2.14_x86_64.deb: Debian binary package (format 2.0), with control.tar.gz, data compression gz/ 

We find that vagrant_2.2.14_x86_64.deb is a Debian binary package (format 2.0), with control.tar.gz, data compression gz/

File Globbing and Wildcards

File globbing is a feature provided by the Unix/Linux shell to represent multiple filenames by using special characters called wildcards. Wildcards are essentially symbols which may be used to substitute for one or more characters. 

File Globbing Examples

  1. rm *: Delete all files in current working directory.
  2. ls l?st: List all files with names beginning with l followed by any single character and ending with st.
  3. rmdir [a-z]*: Remove all directories whose name starts with a letter.

Types of Wildcards

There are three characters that can be used as wildcards in Linux:

  1. (asterisk): which represents zero, one or more occurrences of any character.
  2. (question mark): which represents a single occurrence of any character.
  3. [ ] (bracketed characters): which represents any occurrence of the character(s) enclosed in the square brackets. It is possible to use different types of characters whether numbers, letters, other special characters. For example, the expression [0-9] matches all digits.

Wildcards are very useful as they can be used with commands such as cpls or rm.

The Asterisk

An asterisk (*) matches zero, one or more occurrences of any character.

Using an asterisk wildcard with the ls command;

$ ls *.txt
anotherlist.txt  classified.txt  Nextlist.txt  text1.txt  text2.txt  text3.txt  text4.txt  text.txt  troubleshoot3.txt

In the above output, it lists all files that end with .txt.

Using an asterisk wildcard with the cp (copy) command;

$ cp -rv Dir1/* Contents
'Dir1/listings.txt' -> 'Contents/listings.txt'

In the above example, all the contents of Dir1 is copied into Contents.

Using an asterisk wildcard with the rm (delete) command;

$ rm -v *ext*
removed 'Nextlist.txt'
removed 'text1.txt'
removed 'text2.txt'
removed 'text3.txt'
removed 'text4.txt'
removed 'text.txt'

The filenames prefixed with zero, one or more occurrence of any character, followed by the letters ext and ending with zero, one or more occurrence of any character will be removed.

The Question Mark

The question mark (?) matches a single occurrence of a character.

Let’s list the contents of our current directory with ls command;

$ ls
anotherlist.txt  classified.txt  Contents  Dir1  taste.txt  teeth.txt  text4.txt  toast.txt  troubleshoot3.txt  tutor.txt

To list only the files that start with t followed by any other characters and the characters .txt, we use the question mark (?) wildcard:

$ ls t????.txt
taste.txt  teeth.txt  text4.txt  toast.txt  tutor.txt

output files that are prefixed with any two characters followed by the text st.txt.

$ ls ??st.txt
test.txt  tost.txt

Bracketed Characters

The bracketed wildcards matches any occurrence of the character(s) enclosed in the square brackets.

Using bracketed wildcards with the ls command.

$ ls t[aeiou]st.txt
test.txt  tost.txt

Using a bracketed range wildcard with the ls command.

$ ls t[a-z]st.txt
test.txt  tost.txt

Locating Files with find Command

Files progressively grow in number and size. Sometimes it becomes difficult to locate a particular file. Fortunately, Linux provides find to quickly search and locate files.

Syntax:

find STARTING_PATH OPTIONS EXPRESSION
  • STARTING_PATH: defines the directory where the search begins.
  • OPTIONS: controls the behavior and adds specific criteria to optimize the search process.
  • EXPRESSION: defines the search query.

Example:

$ find . -name "tutor.txt"
./tutor.txt

In the above example the starting path in this case is the current directory. The option -name specifies that the search is based on the name of the file. tutor.txt is the name of the file to search.

When using file globbing, be sure to include the expression in quotation marks. i.e

$ find /home/frank -name "*.deb"
/home/frank/Downloads/MyFiles/vagrant_2.2.14_x86_64.deb
/home/frank/Downloads/vagrant_2.2.14_x86_64.deb

The above command finds all files ending with .deb starting from /home/frank/ directory and beneath.

Use find to locate files based on typesize or time. By specifying one or more options, the desired results are obtained in less time.

Switches to finding files based on type include:

  • -type f: file search.
  • -type d: directory search.
  • -type l: symbolic link search.

Other criteria which could be used with find include:

  • -name: performs a search based on the given name.
  • -iname: searches based on the name, however, the case is not important (i.e. the test case myFile is similar to MYFILE).
  • -not: returns those results that do not match the test case.
  • -maxdepth N: searches the current directory as well as subdirectories N levels deep.

Locating Files by type

find also allows to filter a directory hierarchy based on file type:

$ find . -type d -name "Dir1"
./Dir1

Locating Files by Modification Time

find also allows to filter a directory hierarchy based on when the file was modified:

$ find ~/ -name "*.txt" -mtime 0
/home/frank/.config/google-chrome/Profile 6/Service Worker/CacheStorage/579544fd7d0441717f082c9eb123588966aa57ac/index.txt
/home/frank/Downloads/MyFiles/listings.txt
/home/frank/Downloads/listings.txt
/home/frank/Desktop/tost.txt
/home/frank/Desktop/text4.txt
/home/frank/Desktop/tutor.txt
/home/frank/Desktop/Dir1/listings.txt
/home/frank/Desktop/Contents/listings.txt
/home/frank/Desktop/Contents/file1.txt
/home/frank/Desktop/Contents/results.txt
/home/frank/Desktop/Contents/myfile.txt
/home/frank/Desktop/classified.txt
/home/frank/Desktop/teeth.txt

The above command would search for all files in the entire home directory (the starting path is the home directory, i.e.~/) that end with the characters .txt and have been modified in the last zero days. The argument passed to mtime represents the number of days since the file was last modified.

Locating Files by Size

find can also locate files by size. For example, searching for files larger than 200M in /var:

$ sudo find /var -size +200M
/var/crash/_usr_bin_vlc.1000.crash
/var/lib/snapd/snaps/gnome-3-34-1804_66.snap
/var/lib/snapd/snaps/gnome-3-34-1804_60.snap
/var/lib/snapd/seed/snaps/gnome-3-34-1804_21.snap

The above command would require elevated privileges to access directories starting at the base of the system’s directory structure, hence the use of sudo here.

The -size option displays files of sizes corresponding to the argument passed. Some example arguments include:

  • -size 100b: files which are exactly 100 bytes.
  • -size +100k: files taller than 100 kilobytes.
  • -size -20M: files smaller than 20 megabytes.
  • -size +2G:files larger than 2 gigabytes.

Using cpio Command

The cpio command stands for “copy in, copy out”. It is used to process archive files such as .cpio or .tar files.

cpio performs the following operations:

  • Copying files to an archive.
  • Extracting files from an archive.

It takes the list of files from the standard input (mostly output from ls).

Using cpio to create an archive:

$ ls xampp-linux-x64-7.4.11-0-installer.run | cpio -ov > myfile.cpio
xampp-linux-x64-7.4.11-0-installer.run
305501 blocks

The -o option instructs cpio to create an output. The -v (–verbose) displays each file’s name as each file is processed. In this case, the output file created is myfile.cpio. The ls command lists the contents of the current directory which are to be archived.

Using cpio to list an archive’s contents:

$ cpio -itvI myfile.cpio
-rwxr-xr-x   1 frank    frank    156416206 Oct 26 11:01 xampp-linux-x64-7.4.11-0-installer.run
305501 blocks
  • -I: Designates an archive file to use.
  • -i: Copies files from an archive or displays the files within the archive, depending upon the other options employed. Called copy-in mode
  • -t: Displays a list of files within the archive. This list is called a table of contents.
  • -v: Displays each file’s name as each file is processed.

Using cpio to extract the archive:

$ cpio -idv < myfile.cpio
cpio: xampp-linux-x64-7.4.11-0-installer.run not created: newer or same age version exists
xampp-linux-x64-7.4.11-0-installer.run
305501 blocks

The -i option is used to perform the extract. The -d option would create the destination folder. The character < represents standard input. The input file to be extracted is myfile.cpio.

Using dd Command

The dd utility allows you to back up nearly everything on a disk, including the old Master Boot Record (MBR) partitions some older Linux distributions still employ.

Syntax:

dd if=INPUT_DEVICE of=OUTPUT-DEVICE [OPERANDS]

The OUTPUT-DEVICE is either an entire drive or a partition. The INPUT-DEVICE is the same.

Using dd to copy contents from one location to another:

$ dd if=oldfile of=newfile

This command would copy the content of oldfile into newfile, where if= is the input file and of= refers to the output file.

The dd command typically will not output anything to the screen until the command has finished. By providing the status=progress option, the console will display the amount of work getting done by the command.

For example: dd status=progress if=oldfile of=newfile.

Using dd to change data to upper/lower case:

$ dd if=oldfile of=newfile conv=ucase

The above command would copy all the contents of oldfile into newfile and capitalize all of the text.

Using dd to backup entire disk:

$ dd if=/dev/sda of=mybackup.dd bs=4096

The above command will backup the whole hard disk located at /dev/sda to a file named mybackup.dd.

Conclusion

This marks the end of our guide on Managing Files and Directories on Linux Terminal, hope this guide has been helpful. Stay tuned for more LPIC 101 guides.

Other guides on LPIC 101:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

For the many technological marvels we see today to be realized, there are a couple of things that made it […]

Caching is the key word in this article today and we are going to not only understand what it is […]

What is Web Scraping? Before we move forward with this good agenda, let us take a helpful step and get […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.