Linux File Management

In Linux, every data is in the form of files. Doing modification in the file is called file management. Modifications that are done in the Linux files are,

  1. Creating a file/folder
  2. Writing contents in file
  3. Removing a file/folder
  4. Copying a file/folder
  5. Moving a file/folder

Let's learn all the modifications done in a directory/file in Linux.

Creating and removing folders in Linux

We can create directories/folders with the help of the command mkdir.

Syntax :

mkdir <path of the directory>  

In the below image, I have created a folder named chercher after that a file named f2 is created in the chercher folder.
creating-files-linux

If we try to create a file or a directory in a location where we do not have permission, we shall get the permission denied message and it shall not be created. For example, Creating a file in the root directory.
mkdir-rmdir-linux
mkdir-linux

We can remove directories with the help of the command rmdir.

Syntax to remove the directory :

rmdir <path of the directory>  

mkdir-rmdir-linux

Creating folders and subfolders and deleting the folders and subfolders

In the below image a folder named dummyparent is created. After that, a folder called subdir is created under the dummyparent folder. Inside the subdir folder, a folder named childdir is created.

By using the ls -R command you can view the folders as well as subfolders.

Syntax to delete the folders and subfolders :

rmdir -p <Full path of the sub directory>

rmdir-linux

Creating and editing files in Linux

By using touch command, we can only create the files, and by using the vi command we can create the files and edit the contents of the file.

Touch Command in Linux

We can create files in the Linux system using the commands touch, and vi. Using the vi command we can edit the file by typing some contents in the file.
Syntax to create a file :

//To create the file 
touch <filename>
//To create and edit the file  
vi <filename>
  1. -a, change the access time only
  2. -c, if the file does not exist, do not create it
  3. -d, update the access and modification times
  4. -m, change the modification time only
  5. -r, use the access and modification times of the file
  6. -t, creates a file using a specified time
How to Create Multiple Files using the touch command

By using the touch command, you can also create more than one file. For example, the following command will create 3 files named, f1, f2, and f3.
touch-command-linux

Changing File Access and Modification Time
using the touch command

To change or update the last access and modification time of a file called chercher, use the -a option. The following command sets the current time and date on the mentioned file. If the chercher file does not exist, it will create a new empty file with the mentioned name.

touch -a filename

touch-a-command-linux

Avoid Creating New File
using the touch command

Using -c option with touch command avoids creating new files. For example, the following command will not create a file called tech if it does not exist.
touch-c-command-linux

Changing File Modification Time
using the touch command

If you like to change the only modification time of a file called tech, then use the -m option with touch command. It updates the last modification time of the file to the current time.
touch-m-command-linux

Set the Access and Modification time
using touch command

You can explicitly set the time using the -c and -t option with touch command.

Syntax :

touch -c -t YYDDHRmm filename

In the below example the access and modification date and time to a file tech is set as 17:30 (17:30 p.m.) December 10 of 2012. To verify the changes you can use the ls -l command.
touch-c-date-modification-linux

How to Use the time stamp of another File
using the touch command

The touch command with -r option, will update the time-stamp of file chercher with the time-stamp of tech file. So, both files will have the same timestamp.
touch-r-linux

vi command in Linux

Using the vi command we can create a file as well as we can edit the content in the file.
vi-command-linux

To enter some details in the file press "i" and type whatever you want and to save whatever you have written press ESC and type :wq
vi-command-entering-data-linux

To exit the editor without saving press ESC and type :q!
saving-file-without-changes-linux

Adding Text to Files

There three ways to add text to a file are

  • vi,
  • Redirect command output > or >>
  • echo > or >>.

To add some text into the file, the command should be echo "Linux Concepts" > xy, where xy is the file name. Now to add another line to the same file, the command should echo "Linux add text" >> xy.

To show the content of the file, the command cat is used. To create a file, the command touch is used.

If we use the > symbol for adding the second line, the first line shall be wiped out from the file and the file shall be recreated with the new content. To append content to the same file we have to use the >> symbol.

linux-fundamental17

To capture the content returned by a command in a file, we have to use the redirect command >.

linux-fundamental18

Here, the output of the ls -l xy command is redirected to the listing file.

Viewing the contents of the file in Linux

cat command

cat command is used to view the contents in the file.

Syntax :

cat <filename>

cat-command-linux

less command

less command is also used to view the contents of the file but only the format of viewing the content changes.

Syntax :

less <filename>

Trying to view the content of file sample using the less command

less sample

As soon as you pass the command the below prompt opens if you want to close the prompt press f1 so that you can come out of the prompt.
less-output-linux

more command

more command is also used to view the contents in the file. The difference between the more command and the cat command is in more command you can even change the page.

Syntax :

more <filename>

more-command-linux

nl command

nl command is used to view the contents in the list format.

Syntax :

nl <filename>

nl-command-linux

Copy command in Linux

To copy a file from one folder to another folder copy command is used. The copy command doesn't remove the original folder.

Syntax to copy a file :

cp  <source folder/filename>   <destination folder>

cp-comman-linux

cp -r

Option 'r' with the copy command can be used to copy a directory including all its content from a source directory to the destination directory.

cp -r <source folder/filename>   <destination folder>

cp-r-command-linux

Copy multiple files or directories

Multiple files or directories can be copied to a destination directory at once. In this case, the target must be a directory. To copy multiple files you can use wildcards (cp *.extension) having the same pattern.

cp *.<extension> <destinationDirectory>  

cp-extension-linux

cp -i command

To overwrite the file the command cp with the option -i is used.

cp -i <Source filename> <Destination filename>

cp-overwriting-file-linux

cp -v command

To copy the file by displaying the details the command cp with the option -v is used.

cp -v <Source filename> <Destination filename>

cp-v-command-linux

cp -R command

To recursively copy the folder and its contents the command cp with the option -R is used.

cp -R <source folder> <Destination folder>

cp-r-linux

Remove files in Linux

We can remove the files by using the rm command.

Syntax :

rm <path of the file>

Options such as -rf can be used to recursively delete the files.
creating-removing-files-linux

Move command in Linux

We can move the file from one folder to the other folder by using the below syntax,


Syntax :

mv  <source folderpath/file to copy>   <destination folder path>

Move command copies the contents to the new file and deletes the original file.

mv-command-linux

mv *

This command is used to move all the files from the current directory to another specified directory at once.
move-command-linux

mv -u

When you use the '-u' option, files that already exist in the destination directory will be automatically skipped and all the other files will be copied.
mv-u-command-linux

Linux Rename File and Directory

To rename a file the commands are mv and rename.The rename command is not used much since this command works differently and it works differently on different distributions of Linux.

When you want to rename a large group of files at once then you can use rename command instead of mv command.
rename-command-linux

Once a file is created, the operating system assigns a number or a pointer to that file on a hard disk. This is called the inode.

A soft link is a link that is erased if a file is removed or renamed. The command to create a soft link is ln -s.

ln -s <filepath>

A hard link is a link that will not be affected if a file is removed or renamed. The command to create a hard link is ln.

ln <filepath>

Let us create a file in the current directory with the touch command and create a soft link to that file in the tmp directory.

virtual-box98

The soft link got created. Now let us write some text inside the abc file. The same content shall be reflected inside the soft link of that file.
soft-linux-linux

To get the inode of files we have executed the command ls -ltri. The inode created for the actual file abc and its soft link is different.
virtual-box100

If the actual file abc is deleted with the rm command, then the soft link associated with that also gets removed by default since the source of the link is removed.
softlink-linux

Let us create a file xyz in the current directory with the touch command and create a hard link to that file in the tmp directory.
virtual-box102

The hard link got created. The content inside the xyz file shall be reflected inside the hard link of that file.
hard-link-linux

If the actual file xyz is deleted with the rmcommand, then the hard link associated with that do not get removed even though the source of the link is removed.
hardlink-linux

Wild Cards in Linux

A wild card is a character that can be utilized as a replacement for any class of characters in a search. Some of the wild cards are * which represents zero or more characters,? which represents a single character and [] which denotes a range of characters.

The other wild cards are which represent escape character, ^ which represents the start of the line, and $ which represents the end of the line.

Let us create five files that have digits 1 to 5 and start with abc. The file names end with -xyz. The command should be touch abc{1..5}-xyz.
virtual-box93

Now let us list all the files which start with abc. The command should be ls -l abc*.

ls -l abc*

virtual-box94
Let us remove all the files which end with xyz. The command should be rm *xyz.

rm *xyz

virtual-box95
Let us now get a listing of all files whose names contain bc. The command should be ls -l ?bc*.

ls -l ?bc*

virtual-box96

Troubleshoot File Issues in Linux

We may not be able to delete, copy, rename, or move a file if there are issues in the following parameters:

  • The file does not exist.
  • Absolute vs Relative path.
  • File Type.
  • Permissions.
  • Hidden file.
  • Command syntax.

Finding Files and Directories

We can find files and directories in the Linux system with the help of the commands find and locate. While find is used to search a particular residing inside a large hierarchy. The locate command can help in small searches.


To locate a file with the help of name using find, the command should be find <from location> -name <filename>. To locate a file using locate, the command should be locate <filename>.

virtual-box91

Thus the Practicefile file resides under the Practice directory which is inside /home/saby.

virtual-box97

The absolute path of the Practicefile is /home/saby/Practice/Practicefile.

Difference Between Find and Locate

  • The locate command utilizes an existing database which should be updated at a regular interval of time. The file system goes over the file system to identify files.
  • The locate command is faster in execution than find. But it yields incorrect results if the database is not getting updated at a regular interval of time.
Please note: To modify the database we should run the updatedb command from the root. If we do not run this command, then the database gets updated automatically after some time.

virtual-box92

In the above image, the find command returned the relative path of the Practicefile. But the locate command does not locate since the database has not been updated yet.

Compare Files in Linux

The command diff file1 file2 shall compare two files line by line and the cmp file1 file2 compares two files byte by byte.

linux-fundamental56

Compress and Uncompress Files in Linux

Some of the compounds to compress and uncompress are listed below:

  • tar - It clubs a group of files and puts them in a container.
  • gzip - It compresses a file.
  • gzip -d/gunzip - It uncompresses a file.

The command tar cvf tarfile.tar /home/saby/Pictures shall club all the files and directories located inside /home/saby/Pictures and store them inside the tarfile.tar file.

linux-fundamental57

To untar the tarfile.tar the command is:

tar xvf tarfile.tar

To compress the file, tarfile.tar the command is:

gzip tarfile.tar

To uncompress the zipped tarfile.tar.gz file the command is:

gzip -d tarfile.tar.gz

Truncate File Size in Linux

The truncate command is used to shrink or extend the size of a file to a particular size.

The command truncate -s 27 filename shall shrink the file size to 27 and also wipe out some of the contents of the file.

linux-fundamental58

The command truncate -s 50 filename shall extend the file size to 50 but the original contents of the file shall not be still available.

linux-fundamental59

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions