Many file commands can operate on many different types of items because Linux treats them all as files.
Linux treats various entities such as regular files, directories, symbolic links, pipes, sockets, and device files as files.
file Command β
The file command is used to determine the type of a file. It analyzes the contents of a file and returns a description of its type.
file <filename>sujith@sujith-Latitude-7490:~$ file /home/sujith/Downloads/Print\ Resume.pdf
/home/sujith/Downloads/Print Resume.pdf: PDF document, version 1.4, 1 page(s)
sujith@sujith-Latitude-7490:~$ file /etc/passwd
/etc/passwd: ASCII text
sujith@sujith-Latitude-7490:~$ file -i /etc/passwd
/etc/passwd: text/plain; charset=us-asciiThe -i flag adds MIME type information along with the file description.
Common File Commands β
pwd (Print Working Directory) β
pwd - Displays the current working directory. ~ tilde character at the start of a path means the current users home directory
~/data refers to /Users/sujith/data, useful for absolute path typing.
cd (Change Directory) β
cd - Used to change the current directory.
cd /home/user/Documents
cd ~ # Go to the home directory
cd .. # Go to the parent directory
cd - # Toggle to previous directory
cd / # goes to root directory
cd ../.. # goes up two levels (parent of parent)ls (List Directory Contents) β
Lists files in the current directory with several options:
mv (Move or Rename Files) β
Used to move or rename files and directories.
-f: Force move (overwrite without prompting).-i: Interactive move (prompt before overwriting).-n: Do not overwrite existing files.
mv oldfile.txt newfile.txt
mv file1 /home/user/dir/ # Move file1 to a directorycp (Copy Files) β
Copies files or directories.
-r: Recursive copy (used for directories).-b: Create backups of each destination file.-L: Follow symbolic links.-p: Preserve the original fileβs metadata (permissions, timestamps).-v: Verbose mode (shows each step).-I-sCreate hard/symbolic link rather than physical copy-ucopy only if source is newer than the destination or destination missing
cp file1.txt file2.txt # Copy a file
cp -r dir1 dir2 # Copy a directory recursivelyrm (Remove Files) β
Deletes files and directories.
-f: Force removal (no confirmation).-i: Interactive removal (prompt before each deletion).-r: Recursive removal (for directories).
rm file1.txt
rm -r dir1 # Remove a directory and its contentsmkdir (Make Directory) β
Creates a new directory.
mkdir newdirrmdir (Remove Directory) β
Removes an empty directory.
rmdir emptydircat (Concatenate and Display Files) β
Displays the contents of files.
-n: Add line numbers to output.-T: Show tab characters as^I.
cat file.txt
cat -n file.txt
# Display with line numbersless (View File Content Page by Page) β
Displays a file's content one screen at a time.
-c: Clear the screen before displaying content.-f: Open non-regular files.
less file.txtmore (View File Content Page by Page) β
Similar to less, but with fewer features.
-num #: Specify screen size in rows.+#: Start viewing at a specific line number.
more file.txt
more -10 file.txt # View file starting at line 10head (Display the First Part of a File) β
Displays the first 10 lines of a file by default.
-n #: Specify the number of lines to display.-c #: Display the first number of bytes.
head file.txt
head -n 5 file.txt # Display the first 5 linestail (Display the End of a File) β
Displays the last 10 lines of a file by default.
-n #: Specify the number of lines to display.-c #: Display the last number of bytes.
tail file.txt
tail -n 5 file.txt # Display the last 5 linesfind (Locate Files) β
Searches for files based on conditions like name, type, size, etc.
find /home/user/ -name "*.txt" # Find all .txt files
find / -type d -name "mydir" # Find a directory named "mydir"cmp (Compare Files) β
Compares two files byte by byte.
-i: Ignore case differences.-E: Ignore tabs.-Z: Ignore trailing space-b: Ignore white space-B: Ignore blank lines
cmp file1.txt file2.txtcut (Remove Portions of Each Line) β
Extracts parts of lines from a file based on specified delimiters.
-b: Select bytes.-c: Select characters.-d: Specify a delimiter.-f: Select specific fields.
cut -d -f1 /etc/passwd
# Extract first field of /etc/passwdwc (Word Count) β
Counts lines, words, and characters in a file.
-c: Count characters.-w: Count words.-l: Count lines.
wc file.txt
wc -l file.txt # Count lines in filetouch (Create/Modify File Timestamps) β
Creates a new empty file or updates the access/modification timestamp of an existing file.
-a: Update access time.-m: Update modification time.
touch newfile.txt # Create a new empty fileDirectory Stack Commands β
If you need to manage multiple directories you frequent, you can use the directory stack:
pushd <dirname>: Adds a directory to the stack and changes to it.popd: Removes the top directory from the stack and switches to it.dirs: Displays the contents of the directory stack.
General Utility Commands β
File and Text Handling β
cat- Display or concatenate file contentstee- Redirect output to a file and screen simultaneouslywc- Count lines, words, and characterssort- Sort lines in a file
headβ Show the first few lines of a filetailβ Show the last few lines of a fileuniq- Remove or count duplicate linescutβ Remove sections from lines of filespasteβ Merge lines from multiple filestrβ Translate or delete characterssplitβ Split a file into piecesmore/less- Paginate file content for viewingxargsβ Build command lines from input
File Management β
cpβ Copy files and directoriesmvβ Move or rename files and directoriesrmβ Remove files or directorieslsβ List directory contentstouchβ Create an empty file or update its timestampmkdir- Create directoriesrmdir- Remove empty directories
Permissions and Ownership β
chmod - Change file permission chown - Change file owner chgrp - Change file group ownership umaks - Set default permission mask
Searching and Pattern Matching β
awkβ Pattern scanning and processing languagegrep- Search for patterns in filesegrep- Extended grep (supports more regex)find- Search for files and directoriessedβ Stream editor for filtering and transforming textlocateβ Quickly find files by namewhichβ Shows the location of executablesstat- Detailed file information
Compression & Archiving β
tar- Archive multiple files into one
gzip/gunzipβ Compress and decompress fileszip/unzipβ Compress and extract zip archivesbzip2/bunzip2β Alternative compression utility
System Information and Monitoring β
dateβ Show or set the system date/timecalβ Display a calendarwhoamiβ Show current useruptimeβ Show system uptimehostnameβ Show or set systemβs hostnametopβ Real-time system process viewerpsβ Snapshot of current processesuptimeβ System uptime and load averagedfβ Disk space usageduβ Disk usage of files/directories
