Bash commands Flashcards
List contents of the directory
1) Display long form
2) Print one entry per line
3) Recursively list subfiles
4) Show hidden (dot) files
ls
1) ls -l
2) ls -1
3) ls -R
4) ls -a
Prints text to the cmd window
printf
echo
Improved echo that allows for formatting
Creates a file
touch
Update the access and modification times of each FILE to the current time
A FILE argument that does not exist is created empty, unless -c or -h is supplied
Create a directory
Create a directory with a subdir path
mkdir
Specify multiple directories
mkdir -p
Find out what a command does by printing manual or get help for a command
man
Print working directory
pwd
Change directory
1) One level up
2) Current Dir
3) Home Dir
cd
1) cd ..
2) cd .
3) cd ~
Move or rename a directory
mv
If filepath is the same, renames the file name
Remove empty directories
rmdir
Specify multiple directories
Locate a specific file or directory
locate
You can keep your search broad if you don’t know what exactly it is you’re looking for, or you can narrow the scope by using wildcards or regular expressions.
View the contents of a text file - displayed one page at a time
1) Go down
2) Quit
3) Go to the next file
less
Specify multiple filenames
1) spacebar to scroll down
2) :q
3) :n
Takes the output of a command and saves it to a file
Using the redirect operator >
echo Test > test.txt
head -n 5 filename.csv > head.txt
1) Read a file in CLI
2) Display contents with line numbers create a file, and concatenate files
3) Concatenate the outputs of two files and redirect it to a new file
4) Create a new file using the cat command
cat
cat -n
cat file1.txt file2.txt > combinedfile.txt
cat > file_name.txt
* type text, ENTER, ctrl + D to save the file
1) Print a list of commands run recently
2) Rerun a command based on its history number
3) Clear history
history
!600
history -c
1) Show lines containing specific values
2) Show lines that don’t match - invert the match
3) Print line numbers for matching lines
4) Print count of matching lines
grep
grep -v
grep -n
grep -c
Count the records in a file – lines
Count the records in a file – characters
Count the records in a file – words
Count records that match a term using pipe
wc -l
wc -c
wc -w
grep | wc -l
Specify many files using a wildcard operator
folder/*
head -n 3 *.txt
Prints the first 3 lines of all .txt files
Pipe | operator
Takes the output of a function and passes it as the input to another
1) Reads the start of the file
2) Reads the end of a file
3) Specify number of lines to read
head -n 3
tail -n 3
Using the -n option
Exit out of a session
exit
Clear your terminal window
clear
View current processes running in bash
ps
Copy files / directories
cp old_name new_name
Add a temporary alias
Remove an alias
alias c=”clear”
General syntax: alias =”command_name”
unalias
Open up the nano text editor
1) Write out to a file name
2) Exit nano editor
nano
1) ^O
2) ^X
Kill the current (foreground) process
^c
Remove an entire directory
rm -rf
Recursive and force
Search a previously executed command in the command history
^r then enter search terms
View all environment variables
printenv
Execute a shell script
./script_name.sh