GNU and Unix Commands Flashcards
make
Utility to read a source configuration file and maintain a group of files or programs
env
Shows the current session environment variables
pwd
Print the path of the current working directory
uname
Prints system information
uname -n
Node name
uname -s
Kernel name
uname -v
Kernel version
uname -r
Kernel release
uname -m
Machine/CPU info
uname -p
Processor info
uname -i
Hardware info
uname -o
Operating system name
uname -a
Print all info
export
Used to set an environment variable
export HOME = /home/user
unset
Used to remove an environment variable
unset $HOME
man (1)
Executable programs or shell commands
man (2)
System calls
man (3)
Library calls
man (4)
Special files (usally found in /dev)
man (5)
File formats and conventions
man (6)
Games
man (7)
Miscellaneous
man (8)
System administration commands
man (9)
Kernel routines
fmt
Reformats streams or files for display as indicated
join -t :
Will join lines on occurrences of :
join -1 FIELD
or
join -2 FIELD
Will join on the field in file 1 or in file 2
pr
Convert files for printing
split -l 2 file.txt name
Will split file.txt 2 lines at a time and name the output files namea, nameb, namec…
unexpand
Converts spaces to tabs
expand
Converts tabs to spaces
cut -f1 -d: myfile
Will return the first field of each line in myfile, as separated by colons. For example, if a line is “Matt:Miller:Man”, then “Matt” would be returned.
cut -c2 test.txt
Will display only those letters in the 2nd column of test.txt
cut -c3-6 test.txt
Will display the 3rd through 6th characters of each line
head -c 10 file.txt
Output the first 10 bytes of file.txt
head -n 10 file.txt
Output the first 10 lines of file.txt
nl
Add line numbers to a file for display or redirect to another file
sed ‘s/parttime/fulltime/’ myfile
Will replace the first occurrence of “parttime” in each line of myfile with “fulltime” (Not in the file, just the stream!)
sed ‘s/parttime/fulltime/w promotions.txt’ myfile
Will replace the first occurrence of “parttime” in each line of myfile with “fulltime” and write it to the file promotions.txt
sed ‘/fulltime/w fulltime.txt’ myfile
Will not replace anything, but will write all lines with “fulltime” in them to fulltime.txt
sed ‘s/]*>//’ myfile
Will substitute anything that starts with “”, and ends with “>”
tail -c 10 file.txt
Will output the last 10 bytes of file.txt
tail -n 10 file.txt
Will output the last 10 lines of file.txt
uniq -d
Only print duplicate lines
uniq -u
Only print unique lines
uniq
Prints all lines of a file, but merges duplicates to the first occurrence
od
Dump files in octal format
paste
Merge lines of files
sort file.txt
Outputs the contents of file.txt in alphanumerical order
tr
Translate/squeeze/delete characters from standard input writing to output
wc -l file.txt
Shows number of lines in file.txt
wc -c file.txt
Shows number of bytes in file.txt
wc -m file.txt
Shows number of characters in file.txt
wc -w file.txt
Shows number of words in file.txt
cp -a file1 file2
Makes an archive of file1 called file2
cp -r mydir mynewdir
Will copy the contents of mydir to mynewdir
cp -u
Copy only when the SOURCE file is newer than the destination file or when the destination file is missing
cp –backup
Will backup each destination file
cp -d
Will preserve links
mv file1 file2
Will rename file1 to file2
mv -f
Will not prompt to overwrite
mv -i
Interactive, prompt for overwrite
mv -n
No clobber, do not overwrite existing files
touch -a
Updates the access time only
touch -c
Do not create file if it doesn’t exist
touch -d
Parse string and use it instead of current time
touch -m
Change only the modification time
touch -r file1 file2
Makes file2’s access and modify times the same as file1’s
touch -t
Use specified timestamp
dd if=/dev/zero of=file.iso bs=1024 count=1
Reading from /dev/zero (which just contains zeroes) we want to write to file.iso 1024 bytes (1KB) one time.
dd if=myfile conv=ucase
Writes the contents of myfile to standard output in all caps (conv = lcase would do lowercase)
gzip file1
Compress file1
gzip -d
Decompress file1
gzip -f
and
gunzip -f
Force
gzip -l
and
gunzip -l
List things about a compressed file
gzip -q
and
gunzip -q
Quiet
gzip -t
and
gunzip -t
Test
gzip -v
and
gunzip -v
Verbose
mkdir -p first/second/third
Will create the chain of directories /first/second/third
tar -d
Find differences between archive and file system
tar –delete
Delete from archive
tar -c
Create new archive
tar -A
Append tar files to an archive
tar -r
Append files to the end of an archive
tar -u
Only append files newer than copy in archive
tar -t
List contents of an archive
tar -x
Extract files from an archive
tar -z
Filter through gzip
tar -j
Filter through bzip2
tar -J
filter through xz compression
file filename
Determines the type of file that filename is
bzip2
Compression utility used in conjunction with tar for archiving
ls | tee -a file2
Will append the result of the “ls” command to “file2”
ls | tee mynewfile
Will write the results of “ls” to the file ‘mynewfile’ and also print the result of the “ls” command to stdout
xargs
Reads from standard input and compiles the input into separate arguments.
By default, assumes the arguments are separated by any white space
xargs -a
Reads items from a file instead of stdin
xargs -0
Input items are temrinated by a null character instead of by white space
xargs -d
Input items are terminated by the specified character
jobs
Command that displays minimal info about processes associated with the current session
ps
By default, displays all processes that were run from this terminal
ps -Ae
Displays all processes on the system
ps -u matt
Display processes run by the user matt
ps -w > file.txt
Will write the output of ps to file.txt
uptime
Finds uptime and displays load average
How to put a process in background?
Ctrl+Z
fg
Used to send a program back to foreground
kill pid
Stops the process with the process ID pid
nohup
Used to run a command immune to hangups
killall processname
Kills all processes called processname
free
Show free memory and swap (by default in kilobytes)
free -b
Shows the stats in bytes
free -m
Shows the stats in megabytes
free -s 2
Will show the stats every 2 seconds
Signal 1
SIGHUP 1 HANGUP
Signal 2
SIGINT 2 INTERRUPT FROM KEYBOARD
Signal 9
SIGKILL 9 KILL SIGNAL
Unblockable, the harsher kill signal
Signal 15
SIGTERM 15 TERMINATION SIGNAL
The nicer kill signal
Signal 17, 19, 23
SIGSTOP STOP THE PROCESS
When a child process exits from a parent process it sends which signal?
1
nice programname
Will run programname with a nice value of 10
renice -n -20 -p 1650
Will set the nice value to -20 for the process with PID 1650
nice -n 5 programname
Will run programname with a nice value of 5 (can also just do “nice -5 programname”) (better to do the -n though so that negative values are less confusing)
renice -n -20 -u root
Will set all of roots processes to have a nice value of -20
pgrep processname
Will find all process ids of processes running called processname
Range of nice values
-20 is highest priority, 19 is lowest priority
While running top, k
Kills processes
While running top, q
Quits top
While running top, r
Used to renice process
While running top, s
Used to change update rate
While running top, P
Sort by CPU usage
While running top, M
Sort by memory usage
top -d
Specify delay between updates
top -p
Can specify up to 20 specific PIDs
top -n 2
Display 2 updates then quit
top -b
Batch mode
sort -f
Sorts output, ignoring case
grep ^hello myfile
Will return all lines in myfile that begin with hello
grep -c hello myfile
Gives a count of the number of lines in myfile that contain hello
grep hello$ myfile
Will return all lines in myfile that end with hello
grep [abc] myfile
Will return all the lines in myfile that contain a, b, or c
grep [a-m] myfile
Will return all lines in myfile that contain one of the letters in the range from a to m
grep [1-9] myfile
Will return all lines of myfile that contain one of the numbers in the range 1 to 9
grep -f grepinput myfile
Uses the content of the file grepinput as the regular expression we are searching for in myfile
grep -l word mydir
Will search mydir to find files that contain word within them, NOT in the filename
grep -r word mydir
Will recursively search for filenames containing word starting at mydir
egrep ‘hello.*world’ myfile
Will return all lines in myfile that contain ‘hello’ and ‘world’ in that order
grep -i
Searches case-insensitively
egrep ‘hello|world’ myfile
Will return all lines that contain ‘hello’ or ‘world’ or both
grep -v
Will return all lines that do NOT contain the term we are searching for
fgrep
Used to match patterns literally, i.e. no characters are treated as special characters
While in vi command mode, l (lowercase L)
Moves cursor one character to right
While in vi command mode, h
Moves cursor one character to left
While in vi command mode, j
Moves cursor one character down
While in vi command mode, k
Moves cursor one character up
While in vi command mode, yy
Will yank entire line
While in vi command mode, p
Will paste starting on the line after your cursor
While in vi command mode, P
Will paste starting on line before your cursor
While in vi command mode, 5G
Will move to line 5
While in vi command mode, G
Moves cursor to beginning of last line in the file
While in vi command mode, L
Moves cursor to beginning of last line on the screen (not last line of file)
While in vi command mode, H
Moves cursor to first line on the screen (not first line in the file)
While in vi command mode, o
Creates a new line below your current position and opens insert mode
While in vi command mode, /word
Will search forward in the file for the word “word”.
Use “n” to move to the next occurrence
While in vi command mode,
?word
Will search backward in the file for the word “word”
While in vi command mode, R
Go into insert mode and overwrite text as you type
While in vi command mode, cw
Remove current word under cursor and go into insert mode
While in vi command mode, cc
Remove entire line you are on and go into insert mode
While in vi ex mode, %s/user/root
Will replace the first occurrence of “user” on each line of the file with “root”
While in vi ex mode, %s/user/root/g
Will replace all occurrences of “user” with root; adding the g means “global”
While in vi ex mode, e /file.txt
Will load the new file.txt into the vi editor for editing. This will only occur if the current changes to the file being edited are saved
While in vi ex mode, r
Allows you to bring contents of an old file into a new one
While in vi ex mode, !
Allows you to run shell commands from within vi example: !ls /etc
While in vi command mode,
O
Add line above cursor and go into insert mode
While in vi command mode,
A
Append text at the end of the line
While in vi command mode,
a
Append text after the cursor
While in vi command mode,
I
Insert text at the beginning of the line