Once you are equipped with the basic commands in linux, the next step will be learning file operations in linux using commands. For a primer on basic commands check linux basic commands.

Have you ever tried to copy,paste,move or remove the files in your linux system using commands? Tough eh? Not really!

file operations in linux

Lets begin with the commands on how to create files and directories.

 

 

FILE OPERATIONS IN LINUX

*Creating empty file

# touch file1

This command creates an empty file file1

*creating a file with text content

# cat > file1
…input line1
…input line 2

To save the file press CTRL+D

*View the file content

# cat file1

Opens the file and shows the contents

*Append (add lines to the bottom) the file with more number of input lines

# cat >> file1
…new input line

Save with CTRL +D

*Creating a directory

# mkdir dir1

The above command creates a directory dir1 . Let me remind you a directory is similar to the folder.

*Creating multiple directories in a single line of command

# mkdir dir1 dir2 dir3

It will create three directories dir1 dir2 and dir3

Note: While creating a directory which has space between the names, use double quote(” “). For example, “my directory”.

*Copying the file

# cp file1 dir1

The command above will copy and paste the file1 into directory dir1.
If you copy a file to another regular file, it will overwrite to the destination file.

*Copying a directory

# cp -r dir1 dir2

The option ‘-r’ is used to copy the directory along with its contents and subdirectories.

*Moving a file or directory

# mv file1 dir1
or
# mv dir1 dir2

Its similar to cut and paste operation.

Note: Renaming a file is similar to moving the old file to a new file with different name in same location.

*Deleting a file

# rm file1

use the option “-f” to force delete.

*Deleting a directory

#rm -r dir1
or
#rm  -rf  dir1 ( to force delete)

Leave a Reply

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