This article was translated using AI.
Manipulating Files and Directories
pwd: print the current working directory as an absolute path.
ls: list the contents of the current directory.
cd: change into the directory you specify.
cd .. # move to the parent directory
cd ~ # jump to your home directory
cd . # stay in the current directory
cp: copy files.
cp source_file destination_file
mv: move or rename files.
rm: remove files.
rmdir: remove a directory.
mkdir: create a directory.
Manipulating Data
cat: print the contents of a file.
less file: view a file one page at a time.
:n: next page.:q: quit.
head: print the first 10 lines of a file.
Tab completion helps fill in filenames quickly.
head -n <number_of_lines> filename
ls -R # list every file and directory below the current location
ls -R -F # same, and mark entries by type
man <command> # open the manual page for a command
tail -n +k filename # show the file from line k to the end
cut -f 2-5,8 -d , values.csv # select columns 2 through 5 and column 8
history # display your shell history
grep <pattern> <filename>
# -c : count matching lines
# -h : suppress filenames in the output
# -i : ignore case
# -l : print only filenames that match
# -n : include line numbers
# -v : show non-matching lines
Combining Tools
Use the pipe operator | to connect commands.
tail -n 5 seasonal/winter.csv > /home/repl/last.csv # redirect output into a file
tail -n 2 seasonal/winter.csv > bottom.csv
cut -d , -f 2 seasonal/summer.csv | grep -v Tooth
cut -d , -f 2 seasonal/summer.csv | grep -v Tooth | head -n 1
# wc : word count (-c characters, -w words, -l lines)
grep 2017-07 seasonal/spring.csv
# 2017-07-10,incisor
# 2017-07-16,bicuspid
# 2017-07-23,bicuspid
grep 2017-07 seasonal/spring.csv | wc
# 3 3 59
grep 2017-07 seasonal/spring.csv | wc -l
# 3
cut -d , -f 1 seasonal/*
cut -d , -f 1 seasonal/*.csv
cut -d , -f 1 seasonal/winter.csv seasonal/spring.csv seasonal/summer.csv seasonal/autumn.csv
cut -d , -f 2 seasonal/winter.csv | grep -v Tooth | sort -r
cut -d , -f 2 seasonal/winter.csv | grep -v Tooth | sort | uniq -c # remove duplicates and count them
Ctrl + C: stop a running command.
wc -l seasonal/*.csv | grep -v total | sort -n | head -n 1
Batch Processing
echo $OSTYPE
# linux-gnu
testing=seasonal/winter.csv # no spaces when assigning shell variables
head -n 1 $testing
Use loops to process sets of files.
for filetype in docx odt pdf; do echo $filetype; done
for filename in people/*; do echo $filename; done
datasets=seasonal/*.csv
for filename in $datasets; do echo $filename; done
for file in seasonal/*.csv; do grep 2017-07 "$file" | tail -n 1; done
Creating New Tools
Ctrl + K: delete a line.Ctrl + U: restore the deleted line.Ctrl + O: save the file (press Enter to confirm the filename).Ctrl + X: exit the editor.
nano dates.sh
# inside nano: cut -d , -f 1 seasonal/*.csv
bash dates.sh
bash dates.sh