Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Guide to Useful File Manipulation Commands
File manipulation commands are essential tools for navigating, managing, and manipulating files in Unix/Linux systems. Whether you're a software developer, system administrator, or regular user, mastering these commands will significantly improve your productivity and efficiency when working with files through the terminal.
This guide covers the most commonly used file manipulation commands that every user should know. These commands form the foundation of file management in command-line environments and are crucial for effective system administration and daily computing tasks.
Using ls to List Directory Contents
The ls command displays the contents of a directory. By default, it shows files and directories in the current working directory, but you can specify any directory path as an argument.
$ ls Desktop/cbl 1 1.cbl 2 2.cbl 3.cbl 4.cbl
Common ls Options
| Option | Description |
|---|---|
-l |
Long format shows permissions, owner, size, modification time |
-a |
Shows all files including hidden files (starting with dot) |
-h |
Human-readable file sizes (KB, MB, GB) |
$ ls -lha
total 108K drwxr-xr-x 17 papan papan 4.0K Mar 4 02:09 . drwxr-xr-x 3 root root 4.0K Feb 17 20:53 .. drwxrwxr-x 2 papan papan 4.0K Feb 25 01:13 .aws
Using cp to Copy Files
The cp command creates copies of files or directories. It requires a source and destination argument. This command is essential for creating backups or duplicating files to different locations.
$ cd Desktop/cbl $ ls 1.cbl 2 2.cbl 3.cbl 4.cbl $ cp 2.cbl /home/papan/Documents $ ls /home/papan/Documents 2.cbl
Note: The cp command will overwrite existing files with the same name in the destination directory without warning unless you use the -i (interactive) option.
Using mv to Move and Rename Files
The mv command serves two purposes: moving files between directories and renaming files. It takes two arguments the source and the destination path.
$ mv 3.cbl /home/papan/Documents $ ls /home/papan/Documents 2.cbl 3.cbl
Unlike cp, the mv command removes the original file from its source location. To rename a file in the same directory, specify the new name as the destination.
Creating Files with touch
The touch command creates new empty files or updates the timestamp of existing files. It's the quickest way to create placeholder files.
$ touch 1.txt $ ls 1.txt
If the file already exists, touch updates its modification timestamp to the current time. You can create multiple files simultaneously by providing multiple filenames:
$ touch 2.txt 3.txt $ ls 1.txt 2.txt 3.txt
Using rm to Remove Files
The rm command permanently deletes files from the system. Caution: Unlike graphical file managers, rm doesn't move files to a trash folder deletion is immediate and irreversible.
$ cd Desktop/test $ ls 1.txt 2.txt $ rm 1.txt $ ls 2.txt
Useful rm Options
-iInteractive mode, prompts before deletion-fForce deletion without prompts-rRecursively delete directories and contents
$ rm -i 2.txt rm: remove regular empty file '2.txt'? y
Using cat to Display and Concatenate Files
The cat command displays the entire contents of files in the terminal. It's perfect for viewing small text files quickly.
$ cat 1.txt
Hello World Welcome to India.
You can also use cat to combine multiple files into a single output:
$ cat 1.txt 2.txt 3.txt > combined.txt $ cat combined.txt
Hello World Welcome to India. This is tutorialpoint article. Do you know Linux? Yes I do know.
Using head to View File Beginning
The head command displays the first few lines of a file. By default, it shows the first 10 lines, but you can specify a different number using the -n option.
$ head -n 3 combined.txt
Hello World Welcome to India. Good morning
Using tail to View File End
The tail command shows the last few lines of a file. Like head, it displays 10 lines by default but can be customized with the -n option.
$ tail -n 2 combined.txt
I am fine. Hello this is Somdeb.
Command Summary
| Command | Purpose | Example |
|---|---|---|
ls |
List directory contents | ls -la |
cp |
Copy files/directories | cp file.txt backup/ |
mv |
Move/rename files | mv old.txt new.txt |
touch |
Create empty files | touch newfile.txt |
rm |
Delete files | rm -i file.txt |
cat |
Display file contents | cat document.txt |
head |
Show first lines | head -n 5 file.txt |
tail |
Show last lines | tail -n 5 file.txt |
Conclusion
These fundamental file manipulation commands form the backbone of command-line file management in Unix/Linux systems. Mastering these commands will significantly improve your efficiency when working with files and directories. Practice using these commands regularly to become proficient in terminal-based file operations.
