Linux Flashcards
sudo !!
Runs the previous command, with sudo
.
In general, the two exclamation marks refer to the previous command you just ran.
cd -
Returns to the previous working directory
pushd
popd
pushd
changes into the new directory and pushes the current working directory to a stack.
popd
brings one of the items out of the stack and changes into it
ctrl + z
fg
ctrl + z
sends an app to the background, effectively minimizing it.fg
brings that app out.
ctrl + r
Searches through the commands previously ran. Pressing ctrl + r
again will go to the next best match.
history
Shows the command history.
!
Runs a previously ran command from history. In the Linux world, the exclamation mark is often referred to as bing
.
HISTCONTROL=ignoreboth
If you assign ignoreboth
to this variable, your can prevent commands from showing up in history by adding a space in the beginning,
HISTTIMEFORMAT=”%Y-%m-%d %T “
If you assign “%Y-%m-%d %T “ to this variable, commands stored in the history will have a specific time assigned to them. This could be placed in .bashrc
. The space at the end is for formatting.
cmatrix
A package and a command to show a Matrix-inspired screen saver sort of thing.
ctrl + u
Clears the line.
ctrl + a
Puts the cursor all the way to the front of the line.
ctrl + e
Takes the cursor to the end of the line.
What is the difference between changing commands with ;
and &&
?
When you chain commands with a semicolon, all the commands will run even if one or more of them fail.
On the other hand, when you chain commands with the double ampersand, if it encounters an error it will not run subsequent commands.
tail -f
Shows the content of a file (a log file for example) and updates if anything new is added to the file.
truncate -s 0
Sets the size of a file. If 0 is provided as the first argument, it will empty the file.
column -t
Creates columns out on output. For example mount | column -t
.
Basic data streams in Linux
Standard output (stdout) and 1 is the number designation Standard input (stdin) 0 Standard error (stderr) 2
echo $?
Gives us the return code for the previous command.
0: success
Anything else is considered an error.
find /etc -type f 2> /dev/null
The find command is obvious.
What 2> /dev/null
does is, it redirects all the errors to another file and prevents them from showing up in the output.
The >
symbol is the redirect operator and it will override the file.
But if you use >>
the output will be added to the end of the file.
2 is the designation number for stderr. If you don’t have a number designation, the default is 1 (which is stdout).
/dev/null
is essentially a black hole, anything sent there will be lost forever.
set -x
Prints out all the commands running for calculating the prompt.
After running that, run exec 2>zsh.err
to get stderr into a file.
grep –files-without-match “git” ~/.oh-my-zsh/themes/*
Shows file names that do not match a string.
yes
prints a lot of output
Effectively taking up all the resources
ps
Shows the running processes as part of this particular terminal session
The response is a table with the following format:
PID: the process id
TTY: the terminal that the process is running in. If the process is running as a part of a terminal, it’s going to show here. Otherwise it’s going to show a question mark.
TIME: how much time has the process been utilizing the CPU
CMD: the actual command that is running as part of that process
ps -aux
This form of options is called UNIX style options, which includes a dash at the beginning followed by any number of letters. These letters each represent an option.
Different styles of options a command can get
UNIX Style: ps -aux
BSD style: ps -aux
GNU style: ps –quick-pid
ps x
See all processes running from the current user
There’s a STAT column. Each letter means:
s: the process is a process leader and not a child
S: the process is in an uninterruptable sleep and is waiting for user input
R: the process is actively running
T: the process has stopped. Just like we use ctrl + z to background a process
ps -He
It shows a process hierarchy and process relationships
The H
option formats the CMD column to format the parent-child relationships between the processes.
The e
option tells the command to show all the processes that are running on this system.
ps -axjf
Columns:
PPID: parent process id
SID: session-id
PGID: parent group process id
TPGID: the terminal session-id with which the terminal is associated. If there’s no terminal, a -1 is displayed
UID: user id. 0 will be root. 1000 is the first non-system user created
ps aux
The most popular form of the ps command.
Shows CPU and memory usage. The start time as well.
du -sh
shows a human-readable size for the current woking dir or a specified folder
encode a value to base 64
echo -n | base64
awk ‘{}’
runs a command on a file.
In general, the awk command runs a script on a file
awk ‘{print $1}’
prints the first filed on a file (the columns should be divided by space, as it’s the default separator in awk)
awk ‘{print $0}’
Prints the entire file, since the provided value for the column number is 0 and 0 represents the entire file
awk ‘{print $1,$3}’
Prints the first and third fields of a file
awk ‘{print $NF}’
prints the last filed of a file.
$NF means number of fields and it kinda works like arr.length
awk -F’:’ ‘{print}’
changes the delimiter or separator string that awk uses (default is space)