Managing Files Using Command-Line Tool – Basic Linux Command

Managing Files Using Command-Line tools – Basic Linux Commands


After Completing this section, you will be able to
  • Create Files, Directory and Sub-directory.
  • Copy Files, Directory and Sub-directory.
  • Link Files, Directory and Sub-directory.
  • Move Files, Directory and Sub-directory.
  • Remove Files, Directory and Sub-directory.

Managing Files Using Command-Line Tool – Basic Linux Command

File management involve creating, deleting, copying and moving files. Additionally, directories can be created, deleted, copied, and moved to help organize file logically. When working at the command-line, file management requires awareness of the current working directory to choose either absolute or relative path syntax as most efficient for the immediate task.

Create Directory

The mkdir command creates one or more directories and sub-directories. This command will generating error if the file name already exists or when attempting to create a directory in a parent directory that doesn’t exists. The -p (parent) option creates missing parent directory for the destination.
~]$  mkdir Documents/imp
mkdir: cannot create directory ‘Documents/imp’: No such file or directory

The mkdir failed because Documents was misspelled and the directory Documents doesn’t exists. If the user had user mkdir -p, there would be no error.
~]$  mkdir -p Documents/imp
~]$ ls
Desktop  Document  Documents  Downloads  Music  Picture  Public  Templates  Videos 

Copy Files

The cp command copies one or more file to become new, independent files. The Syntax of Copy command allow copying an existing file to a new file in the current or another directory, or copying multiple files into another directory. In any destination, new file name must be unique. If file name is not unique, the copy command will automatically overwrite the existing file. And if you want to copy non-empty directory, with contents, you must be use -r (recursive) option with this copy command.
~]$ cd Document
Document]$ cp imp1.txt  imp3.txt
Document]$ ls
imp imp1.txt imp3.txt

Move File

The mv command renames file in the same directory, or relocates files to a new directory. Files contents remain unchanged. File moved to a different file system require creating a new file by copying the source file, then deleting the source file. Although normally, transparent to the user, large files may take noticeable longer to move.
~]$ cd Documents
~]$ ls
~]$ mv Document/imp1.txt Documents
~]$ cd Documents
~]$ ls
abhi.txt  imp1.txt

Rename files and Directory

The rm command deletes file, but not directories. If you want to delete a directory with contain sub-directory or file, you must use -r (recursive) option. If you want to delete empty directory, so you can use rmdir command to do that.

~]$ rm -r  Documents/abhi.txt
Documents]$ ls
Document]$ cd ..
~]$ rm -r Documents/imp1.txt
I have tried to highlight Managing Files Using Command-Line tools (Basic Linux Command). However, this is not end of it.