Some very basic linux commands for quick reference.
- ls: list files
ls
, basic listls -l
, one per line, long formatls -a
, list all files, including hidden filels -la
, combination of the twols -lh
, with file size in human readable formatls -lS
, long format list sorted by size (descending)ls -lt
, sort by modification time, newest firstls -ltr
, sort by time in reverse order
- less: open file and read by page
less source_file
, open a file and onle show the first page, lessd
to next page;p
to previous pageg
to start;G
to end/kw
for forward search;?kw
for backword search;n
next andN
previousq
to quit
- cp: copy file or folder
cp /path/to/origin /path/to/copy
, copy filecp -r /path/to/origin /path/to/copy
, copy folder, recursivecp -vr /path/to/origin /path/to/copy
, verbose mode, show it- file name can be specified with wildcard
- mv: move or rename files and directories
mv source target
, move or renamemv -v source target
, verbose modemv -f source target
, overwriting with out confirmation
- rm: remove files or directories
rm /path/to/file
, remove filerm -r /path/to/folder
, remove folder, recursivelyrm -rf /path/to/folder
, remove folder without promptrm -i /path/to/file
, interactive mode, need confirmation- before use rm with wildcards, try it with ls first
- wildcard: match filename
*
, match any characters?
, match any single character[characters]
, match member character of set[abc]
, match a, b or c[:alnum:]
, match alphanumeric characters[:alpha:]
, match alphabetic characters[:digit:]
, match numerals[:upper:]
, match uppercase alphabetic characters[:lower:]
, match lowercase alphabetic characters
[!characters]
, match opposite of[characters]
- I/O Redirection
- stdout: redirect standard output to file
ls
, by default to standard output, like displayls > file_list.txt
, use > to redirect, write models >> file_list.txt
, use » to redirect, append mode
- stdin: get standard input from file
sort < file_list.txt
, use<
as standard inputsort < file_list.txt > sort_file_list.txt
, combine stdin and stdout
- pipelines
|
: stdout of one cmd is fed into the stdin of anotherls -l | less
, powerful pipeline|
ls -lt | head -5
, dispalys 5 newest filesdu | sort -nr
, display list of dirs and space consumed, sort it, largest first
- filter: take stdin, perfrom operation, send results to stdout
sort:
sort filename
, sort a file in ascending ordersort -r filename
, sort a file in descending ordersort -n filename
, sort a file using numeric rather than alphabetic ordersort -u filename
, sort and preserve only unique lines
uniq:
return unique line, needs to be sorted firstsort file | uniq
, display each line oncesort file | uniq -u
, display only unique linessort file | uniq -d
, display only duplicated linessort file | uniq -c
, display unique lines with occurencessort file | uniq -c | sort -nr
grep:
examine lines of input, output lines that contain the patterngrep query_string path/to/file
, search for an eaxct stringgrep -i query_string path/to/file
, in case-insensitive modegrep -rI query_string .
, search current dir recursively, ignore binary filegrep -C 3 query_string path/to/file
, context with 3 linesgrep -c query_string path/to/file
, count of match instead of matching textgrep -n query_string path/to/file
, line number for each matchgrep -l query_string path/to/file
, list of files with matchescat path/to/file | grep query_string
, search stdin using pipeline
head
: output the first part of fileshead -n count_of_lines filename
, first n lineshead -c number_in_bytes filename
, first n bytes
tail
: output the last part of filestail -n count_of_lines filename
, last n linestail -c number_in_bytes filename
, last n bytes
- stdout: redirect standard output to file
- unzip: extract compressed files in a zip archive
unzip file
, extract zip fileunzip compressed_file -d /path/to/put/extracted_file
, extract zip file to folderunzip -l file
, list the contents of a zip file without extractingzip
,unzip
,gzip
,gunzip
,tar
…
- ssh:
ssh user@your_ip
- nohup: Allows for a process to live when the terminal gets killed
nohup command options
nohup command options &
, run in the background
- linux command