1. ls – List Directory Contents
The ls command is one of the most frequently used Linux commands. It lists the contents of a directory, showing all files and subdirectories contained inside.
Without any options or arguments, ls will display the contents of the current working directory. You can pass a path name to list files and folders in that location instead.
Syntax:
- ls -l /home/user/documents
Some of the most useful ls options include:
- -l – Display results in long format, showing extra details like permissions, ownership, size, and modification date for each file and directory.
- -a – Show hidden files and directories that start with . in addition to non-hidden items.
- -R – Recursively list all subdirectory contents, descending into child folders indefinitely.
- -S – Sort results by file size, largest first.
- -t – Sort by timestamp, newest first
2. cd – Change Directory
The cd command is used to navigate between directories. It allows you to move the current working directory to a new location in the filesystem.
When you run the cd command by itself, it will return you to the home directory. You can also pass a specific path to change into. For example:
- cd /usr/local – Changes to the /usr/local directory.
- cd .. – Moves up one level to the parent directory.
- cd ~/pictures – Changes to the pictures folder in your home directory.
Syntax:
cd [directory]
Example:
cd /home/user/documents
This would change the working directory to the “documents” folder under /home/user. Using cd is essential for being able to access and work with files in different locations conveniently.
3. mkdir – Create A New Directory
The mkdir command allows you to create a new folder. You simply pass the name of the directory to create.
Syntax:
mkdir [options] <directory>
This will create a directory called “newproject” in the current working directory.
Some useful mkdir options:
- -p – Creates parent directories recursively as needed.
- -v – Verbose output showing created directories.
Example:
mkdir -v ~/project/code
This would create the “code” subdirectory under “project” in the user’s home folder, with verbose output showing the directory being created.
4. rmdir – Remove Directory
To delete an empty directory, use the rmdir command. Note that rmdir can only remove empty directories – we’ll need the rm command to delete non-empty ones.
Syntax:
rmdir [options] <directory>
Some options for rmdir include:
- -v – Verbose output when deleting directories.
- -p – Remove parent directories recursively as needed.
Example:
rmdir -v ~/project/code
This would delete the “code” subdirectory under “project” while showing verbose output.
5. touch – Create A New Empty File
The touch command is used to create a new empty file instantly. This is useful when you need an empty file to populate with data later.
The basic syntax of touch is:
touch [options] filename
Some useful options for touch include:
- -c – Do not create the file if it already exists. This avoids accidentally overwriting existing files.
- -m – Instead of creating a new file, update the timestamp on an existing file. This can be used to change the modified time.
For example:
touch /home/user/newfile.txt
The above command creates a new empty file called “newfile.txt” in the user’s /home/user directory. If newfile.txt already exists, it will update the access and modification times on the file instead.
6. cp – Copy Files And Directories
The cp command copies files or directories from one location to another. It requires passing a source path and a destination.
The basic syntax of cp is:
cp [options] source destination
Some useful cp options:
- -r – Copy directories recursively, descending into child directories to copy their contents as well. Necessary when copying directories.
- -i – Prompt before overwriting any existing files at the destination. It prevents accidentally overwriting data.
- -v – Display verbose output showing the details of each file as it is copied. Helpful to confirm exactly what was copied.
For example:
cp -r /home/user/documents /backups/
This would recursively copy the /home/user/documents directory and all its contents to the /backups/ directory. The -r option is needed to copy directories.
The cp command is one of the most frequently used file management utilities for copying files and directories in Linux. You’ll find yourself using this command quite often.
7. mv – Move Or Rename Files And Directories
The mv command is used to move files or directories to a different location or rename them. Unlike copy, the files from the source path are deleted after they’ve been moved to the destination.
You can also use the mv command to rename files since you simply need to change the source and destination paths to the old and new name.
The syntax of mv is:
mv [options] source destination
Useful mv options:
- -i – Prompt before overwriting any existing files at the destination location. This prevents accidentally overwriting data.
- -v – Produce verbose output showing each file or directory as it is moved. This is helpful for confirming exactly what was moved.
For example:
mv ~/folder1 /tmp/folder1
The above will move folder1 from the home (~) directory to the /tmp/ directory. Let’s look at another example of using the mv command for renaming files.
mv folder1 folder2
Here, “folder1” is renamed to “folder2.”
8. rm – Remove Files And Directories
The rm command deletes files and directories. Use caution because deleted files and directories cannot be recovered.
The syntax is:
rm [options] name
Useful rm options:
- -r – Recursively delete directories, including all contents inside them. This is necessary when deleting directories.
- -f – Force deletion and suppress all confirmation prompts. This is a dangerous command, as files cannot be recovered when they’re gone!
- -i – Prompt for confirmation before deleting each file or directory, which provides safety against accidental removal.
For example:
rm -rf temp
This recursively deletes the “temp” directory and all its contents without prompting (-f overrides confirmations).
Note: The rm command permanently erases files and folders, so use it with extreme care. If used with sudo privileges, you could also delete the root directory completely, and Linux would no longer function after restarting your computer.
9. find – Search For Files In A Directory Hierarchy
The find command recursively searches directories for files matching given criteria.
The basic syntax of find is:
find [path] [criteria]
Some useful criteria options for find include:
- -type f – Search for only normal files, omitting directories.
- -mtime +30 – Search for files modified over 30 days ago.
- -user jane – Search for files belonging to user “jane.”
For example:
find . -type f -mtime +30
This will find all regular files over 30 days old under the current directory (denoted by the dot).
The find command allows searching for files based on all kinds of advanced conditions like name, size, permissions, timestamps, ownership, and more.
10. du – Estimate File Space Usage
The du command measures the file space usage for a given directory. When used without options, it displays disk usage for the current working directory.
The syntax for du is:
du [options] [path]
Useful du options:
- -h – Display file sizes in human-readable format like K for Kilobytes rather than a byte count. Much easier to parse.
- -s – Only show the total size for a directory, rather than listing each subdirectory and file. Good for summary.
- -a – Show individual file sizes in addition to totals. Helps identify large files.
For example:
du -sh pictures
This will print a human-readable size total for the “pictures” directory.
The du command is helpful for analyzing disk usage for a directory tree and identifying files consuming excessive space.
11. grep – Search Text Using Patterns
The grep command is used to search for text patterns within files or output. It prints any lines that match the given regular expression. grep is extremely powerful for searching, filtering, and pattern matching in Linux.
Here is the basic syntax:
grep [options] pattern [files]
For example:
grep -i "error" /var/log/syslog
This searches the syslog file for the word “error,” ignoring case sensitivity.
Some useful grep options:
- -i – Ignore case distinctions in patterns
- -R – Recursively search subdirectories
- -c – Print only a count of matching lines
- -v – Invert match, print non-matching lines
grep allows you to search files and output for keywords or patterns quickly. It’s invaluable for parsing logs, searching source code, matching regexes, and extracting data.
14. sort – Sort Lines Of Text Files
When you’re working with a lot of text or data or even large outputs from other commands, sorting it is a great way to make things manageable. The sort command will sort the lines of a text file alphabetically or numerically.
Basic sort syntax:
sort [options] [file]
Useful sort options:
- -n – Sort numerically instead of alphabetically
- -r – Reverse the sort order
- -k – Sort based on a specific field or column
For example:
sort -n grades.txt
This numerically sorts the contents of grades.txt. sort is handy for ordering the contents of files for more readable output or analysis.
15. uniq – Report Or Omit Repeated Lines
The uniq command filters duplicate adjacent lines from input. This is often used in conjunction with sort.
Basic syntax:
uniq [options] [input]
Options:
- -c – Prefix unique lines with count of occurrences.
- -d – Only show duplicated lines, not unique ones.
For example:
sort data.txt | uniq
This will remove any duplicated lines in data.txt after sorting. uniq gives you control over filtering repeated text.
16. diff – Compare Files Line By Line
The diff command compares two files line-by-line and prints the differences. It’s commonly used to show changes between versions of files.
Syntax:
diff [options] file1 file2
Options:
- -b – Ignore changes in whitespace.
- -B – Show differences inline, highlighting changes.
- -u – Output differences with three lines of context.
For example:
diff original.txt updated.txt
This will output the lines that differ between original.txt and updated.txt. diff is invaluable for comparing revisions of text files and source code.
17. wc – Print Line, Word, And Byte Counts
The wc (word count) command prints counts of lines, words, and bytes in a file.
Syntax:
wc [options] [file]
Options:
- -l – Print only the line count.
- -w – Print only the word count.
- -c – Print only the byte count.
For example:
wc report.txt
This command will print the number of lines, words, and bytes in report.txt.
18. > – Redirect Standard Output
The > redirection operator redirects the standard output stream from the command to a file instead of printing to the terminal. Any existing contents of the file will be overwritten.
For example:
ls -l /home > homelist.txt
This will execute ls -l to list the contents of the /home directory.
Then, instead of printing that output to the terminal, the > symbol captures that standard output and writes it to homelist.txt, overwriting any existing file contents.
Redirecting standard output is helpful for saving command results to files for storage, debugging, or chaining commands together.
19. >> – Append Standard Output
The >> operator appends standard output from a command to a file without overwriting existing contents.
For example:
tail /var/log/syslog >> logfile.txt
This will append the last 10 lines of the syslog log file onto the end of logfile.txt. Unlike >, >> adds the output without erasing the current logfile.txt contents.
Appending is helpful in collecting command output in one place without losing existing data.
20. < – Redirect Standard Input
The < redirection operator feeds a file’s contents as standard input to a command, instead of taking input from the keyboard.
For example:
wc -l < myfile.txt
This sends the contents of myfile.txt as input to the wc command, which will count lines in that file instead of waiting for keyboard input.
Redirecting input is useful for batch-processing files and automating workflows.
21. | – Pipe Output To Another Command
The pipe | operator sends the output from one command as input to another command, chaining them together.
For example:
ls -l | less
This pipes the output of ls -l into the less command, which allows scrolling through the file listing.
Piping is commonly used to chain together commands where the output of one feeds the input of another. This allows building complex operations out of smaller single-purpose programs.
Archive Commands
Archiving commands allow you to bundle multiple files and directories into compressed archive files for easier portability and storage. Common archive formats in Linux include .tar, .gz, and .zip.
23. tar – Store And Extract Files From An Archive
The tar command helps you work with tape archive (.tar) files. It helps you bundle multiple files and directories into a single compressed .tar file.
Syntax:
tar [options] filename
Useful tar options:
- -c – Create a new .tar archive file.
- -x – Extract files from a .tar archive.
- -f – Specify archive filename rather than stdin/stdout.
- -v – Verbose output showing archived files.
- -z – Compress or uncompress archive with gzip.
For example:
tar -cvzf images.tar.gz /home/user/images
This creates a gzip-compressed tar archive called images.tar.gz containing the /home/user/images folder.
29. rsync – Synchronize Files Between Hosts
The rsync tool synchronizes files between two locations while minimizing data transfer using delta encoding. This makes it faster to sync large directory trees.
rsync syntax syncs source to destination:
rsync [options] source destination
For example:
rsync -ahv ~/documents user@server:/backups/
The above example command recursively syncs the documents folder to server:/backups/, showing verbose, human-readable output.
Useful rsync options:
- -a – Archive mode syncs recursively and preserves permissions, times, etc.
- -h – Human-readable output.
- -v – Verbose output.
rsync is ideal for syncing files and folders to remote systems and keeping things decentrally backed up and secure.
30. sftp – Secure File Transfer Program
The sftp program provides interactive file transfers over SSH, similar to regular FTP but encrypted. It can transfer files to/from remote systems.
sftp connects to a host then accepts commands like:
- sftp user@host
- get remotefile localfile
- put localfile remotefile
This retrieves remotefile from the server and copies localfile to the remote host.
sftp has an interactive shell for navigating remote file systems, transferring files and directories, and managing permissions and properties.
31. wget – Retrieve Files from the Web
The wget tool downloads files over HTTP, HTTPS, and FTP connections. It’s useful for retrieving web resources directly from the terminal.
For example:
wget https://example.com/file.iso
This downloads the file.iso image from the remote server.
Useful wget options:
- -c – Resume interrupted download.
- -r – Download recursively.
- -O – Save to specific filename.
wget is ideal for scripting automatic downloads and mirroring websites.
32. curl – Transfer Data From Or To A Server
The curl command transfers data to or from a network server using supported protocols. This includes REST, HTTP, FTP, and more.
For example:
curl -L https://example.com
The above command retrieves data from the HTTPS URL and outputs it.
Useful curl options:
- -o – Write output to file.
- -I – Show response headers only.
- -L – Follow redirects.
Post a Comment
0Comments