File handling using Linux command line

Below are the basic commands that we need to know for handling files using Linux terminal.

1. cd - Current Directory
Used to navigate through directories(folders)
Note:
It cannot be used for navigating through files(only for folders).
Current Directory that you're in would be displayed on the left side (beginning of each line).
Example: cd Downloads
Bonus Tips:
use cd ../ for navigating up one directory.
use cd for navigating to home directory(root).

2. pwd
Prints the current directory(working directory)
Note:
Print Working Directory
If you confused of the file structure you're working in then, use this command

3. ls
Lists all the directories and files which are direct childrens of the current directory
Example: 
ls
ls -a
Lists all the directories and files which are direct childrens of the current directory(including hidden files).
Example: 
ls -a

4. touch
Created new file on current directory
Example: touch test.txt

5. cat
Used to print the contents of the file on terminal
Note:
While creating file you must include the extension of file type such as .txt or .html
Example: 
cat test.txt

6. mkdir
Used to create a new directory(folder) in the current directory.
Note:
Make Directory
This command also works for windows
A folder name does not end with any of the extension like .txt or .html
Example: mkdir text

7. mv - Move
Used to move file/folder into specified directory.
Syntax: mv <file/folder_name_to_move> <destination_folder_name>
Example:
mv office.txt documents
The above command moves the office.txt file to the documents folder.
Note:
This will only possible if the file/folder to move and the destination folder are in the same working directory(current directory).
It can also be used to change the file/folder name
Syntax: mv <old_file_name> <new_file_name>
Example: mv office.txt list.txt
Explanation: The above command renames office.txt to list.txt

8. cp - Copy
Used to copy files.
Syntax: cp <resource_file> <target_file>
Example: cp office.txt new.txt
The above command copies the text from office.txt, creates new file new.txt and pastes the copied text to the new.txt.
To copy a directory use cp -r <resource_directory> <target_directory>
r refers to recursive copying.

9. rm - Remove
Used to remove/delete file/folder. 
Syntax to remove file:
rm <filename>
Syntax to remove folder: (recursively including child folders and files)
rm -r <folder_name>

10. whereis / which
Find location at which a program is located.
Example: whereis gcc

Thank you for reading this blog so far. Hope you get to know some cool things today. Bookmark this blog so that you can refer the commands at any time you need.

Comments