Skip to main content

Text

cat, zcat, less, more, head, tail, ccat, bat

CommandDescription
cat file.txtConcatenate and display
cat -n file.txtDisplay with line numbers
zcat file.gzDisplay compressed content
ccat file.txtColorize and concatenate
bat file.txtCat clone with syntax
tail file.txtDisplay last N line (10 by default)
tail -f file.txtContinuously display new lines
head file.txtDisplay first N lines (10 by default)
head -n 5 file.txtDisplay first 5 lines
head -5 file.txtDisplay first 5 lines
less file.txtView file with pagination
more file.txtView file page by page

tee

tee allows you to read from standard input and write to both standard output and files simultaneously.

CommandDescription
tee file.txtRead from stdin and write to file.txt, creating or overwriting it
ls | tee list.txtRedirect the ls output to both terminal and list.txt
tee -a file.txtAppend to file.txt instead of overwriting it
ps aux | grep "node" | tee processes.txtlist and save running processes containing node

split

CommandDescription
split file.txtSplit file into parts, by default 1000 lines per file
split -l 100Split into 100-line files
split -b 1MSplit into 1MB files
split -n 3 file.txtSplit into 3 equal parts
split -dUse numeric suffixes
split test.txt -d hiUse numeric suffixes and custom prefix "hi"
split -a 3Use 3-character suffix
cat hi* > concatenated_file.txtConcatenate files starting with "hi"

cut

CommandDescription
cut -f 1,3 file.txtPrint the first and third fields from each line of file.txt
cut -d' ' -f 2-4 file.txtPrint fields 2 to 4 from each line of "file.txt" using space as the delimiter
cut -c 1-5 file.txtSelect and print characters 1 to 5 from each line of "file.txt"

Tab is the defualt delimiter

paste, wc

CommandShort Description
pasteMerge lines of files horizontally
paste file1 file2Merges lines from 'file1' and 'file2'
paste -s file1 file2Merges lines from 'file1' and 'file2' sequentially
paste -d, file1 file2Merges lines with a comma delimiter
wc -l file.txtCount lines in a file
wc -w file.txtCount words in a file

sort, nl, shuf, uniq, tr

CommandDescription
sort file.txtSort lines in a text file
sort -r file.txtSort lines in reverse order
sort -n file.txtSort lines numerically
nl file.txtNumber lines in a file
nl -b a file.txtNumber lines, showing all lines
nl -s "," file.txtNumber lines, custom separator
shuf file.txtShuffle lines in a file
shuf -n 5 file.txtShuffle and display only 5 lines
uniq file.txtDisplay unique lines in a file
uniq -c file.txtCount and display unique lines with count
cat file.txt | tr -s ' ' ','Translate spaces to commas in a text stream

uniq only removes duplicate consecutive lines from the input. To remove all duplicate lines, you can pipe the output to sort and then to uniq again. like sort file.txt | uniq

diff, sha256sum, cp, mv, rm

Certainly, here's a table of the commands you requested in markdown format, with centered text alignment as per your preference:

CommandDescription
diff file1 file2Compares two files and shows differences
sha256sum file.txtCalculates and prints the SHA-256 hash of the file file.txt
cp -rCopies directories and their contents recursively
cp -vCopies files or directories with verbose output
mv -iMoves (renames) files or directories with prompt
rm -rRemoves directories and their contents recursively
mkdirCreates a new directory
rmdirRemoves an empty directory

grep

CommandDescription
grep 'error' log.txtSearch for 'error' messages in a log file
grep -r 'function()' /projectRecursively find files contain 'function()' in the '/project' directory
grep -i 'todo' *.jsSearch case-insensitively for 'todo' in JavaScript files
grep -E '^\d{3}-\d{2}-\d{4}$' data.txtUse extended regex to find social security numbers in 'data.txt'
grep -l 'pattern' *.txtList files containing 'pattern' in the current directory
grep -v 'warning' log.txtInvert the match to display lines without 'warning' in the log file

find

CommandDescription
findSearch for files and directories
find path -name patternFiles with a specific name in the 'path' directory
find /home/user -name "*.txt"All files with the '.txt' extension in '/home/user'
find /etc -type d -name "conf"Locate directories named 'conf' under '/etc'
find /var/log -type f -mtime +7Find files in '/var/log' modified more than 7 days ago
find /usr/bin -executable -type fFind executable files in '/usr/bin'
find /home -user masoudFiles owned by the user 'masoud' under '/home'
find /mnt -size +100MFind files larger than 100MB in '/mnt'
find /var/log -iname "*.log" -exec ls -l {} \;Files in '/var/log' with a case-insensitive name match
find /backup -type f -exec cp {} /backup_archive/ \;Copy found files to '/backup_archive/' directory
find ./ -type f -exec ls {} \; -exec echo "file: {}" \;Having two commands
find / -name 'pycache' -exec rm -r {} +Remove all pycache, pass items as a list to rm

tar, gzip, guzip, xz, unxz

CommandDescription
gzipCompress files using gzip
gzip file.txtCompress the file.txt using gzip
gzip -d file.gzDecompress file.gz using gzip
gunzip file.gzDecompress file.gz using gunzip
xzCompress files using xz
xz file.txtCompress the file.txt using xz
xz -d file.xzDecompress file.xz using xz
unxz file.xzDecompress file.xz using unxz
tarCreate and extract tar archives
tar -cvf archive.tar files/Create a tar archive from the 'files/' directory
tar -xvf archive.tarExtract files from 'archive.tar'
tar -xvf archive.tar -C /Change to / directory and then extract

By default tar reads input from stdin.

streams

Certainly, here's a Markdown table with centered text alignment that lists the Linux commands you mentioned, along with their short descriptions and real-world examples:

CommandDescription
>Redirects standard output to a file
ls > list.txtRedirects ls command output to list.txt
>>Appends standard output to a file
echo "Hello" >> greeting.txtAppends "Hello" to greeting.txt
2>Redirects standard error to a file
ls non_existent_dir 2> error.logLogs error from ls to error.log
2>>Appends standard error to a file
find /non_existent_dir 2>> errors.logAppends errors to errors.log
&>Redirects both standard output and error to a file
curl example.com &> website.logLogs both output and errors from curl to website.log
&>>Appends both standard output and error to a file
git pull &>> git.logAppends both git output and errors to git.log
<>Redirects both input and output from/to a file
cat <> combined.txtReads and writes to combined.txt
command > output.txt 2> error.txtRedirects output to output.txt and error to error.txt
command 2>&1 > output_and_error.txtRedirects both output and error to output_and_error.txt

pipe

CommandDescription
|Redirect output from one command to another
command1 | command2Execute command1 and pass its output to command2
ls | grep .txtList and filter files with a .txt extension
ps aux | grep nginxList and filter processes related to nginx
cat file.txt | lessDisplay file contents using the less pager
dmesg | tail -n 10Display the last 10 kernel log messages
ls | sort -rList files in reverse alphabetical order
find /home | wc -lCount files and directories in /home
du -h | sort -rhList disk usage, human-readable, and sorted
ls | tee file.txtList files and save output to file.txt
ls | head -n 5List the first 5 files in the current directory

xargs

xargs is a Linux command-line tool that takes input from standard input and passes it as arguments to another command. It separates input items by spaces, tabs or newlines by default, but you can specify a different delimiter with the -d option. The output is the result of executing the specified command with the input items as arguments.

By default, xargs will process as many input items as possible in a single execution of the command.

  • -n 1: This option in xargs specifies that only one input item should be used for each execution of the command. It ensures that the command is run once for each input item
  • -I {}: The -I option allows you to specify a placeholder (in this case, {}) to represent where the input item should be placed within the command. It also process one input item at a time
CommandDescription
xargsBuild and execute command lines from standard input
find /path -type f -print | xargs commandExecute command on each file found by the find command
echo arg1 arg2 | xargs commandExecute command with arguments arg1 and arg2
ls *.txt | xargs rmRemove all .txt files in the current directory
cat list.txt | xargs -n 1 echoPrint each line of list.txt using echo (one by one)
ls | xargs -I {} mv {} {}.bakAdd ".bak" extension to all files in this directory (one by one)
find /path -type f -name "*.log" -print0 | xargs -0 rmRemove ".log" files in "/path" with handling special characters
grep pattern f1 f2 | xargs sed -i 's/pattern/replacement/g'Search, replace "pattern" with "replacement" in multiple files
echo file1 file2 file3 | xargs -I % sh -c 'cp % /backup'Copy multiple files to a backup directory
find /path -type f -print | xargs -P 4 -I % gzip %Parallel compression of files in "/path" using 4 processes
ls *.txt | xargs -n 1 -I {} mv {} /destination/Move each ".txt" file to "/destination/" directory
echo file1 file2 | xargs -d ' ' -I % sh -c 'touch %.txt'Create ".txt" files with specified names