Bash Commands Flashcards
vim -r
list swap files
tail ‘file’
shows last 10 lines of the file
vim
open or creates file
mv ‘file1’ ‘file2’
move file to destination or rename file
cp ‘file1’ ‘file2’
copy contents from file1 to file2
rm ‘file’
remove a file
rm -r ‘file’
remove a file recursively
rm -d
remove empty directory
rm – -foo
rm file that starts with foo
find . -name ‘file1’ -type d
find the directory called file1. Without the -type d, it will search for files and directories
diff ‘file1’ ‘file2’
compares files and shows where they are different
sort -n -r ‘file’
sort files, -n for numeric sort and -r for reversing order
rev
reverse string character
chmod +wrx
reach, write and execute permissions
chmod -o -g -a -u
removes permission for others not in group, group, all users and user
lpr ‘filename’
prints the file
grep -i ‘hello’ ‘filename’
search for hello in filenames (this will not be case-sensitive)
grep -r ‘hello’ ‘directory
search recursively for pattern in the directory
sed ‘s/hello/goodbye/g’ ‘filename’
This will replace all occurrences of hello with goodbye in ‘filename’ without modifying the file
sed -i ‘s/dog/cat/g’ file.txt
replaces “dog” with “cat” in file.txt and saves the change
echo “this” | sed ‘s/is/at/g’
replaces “is” with “at” in the input string “this”
mkdir ‘filename’
makes a new directory
rmdir -rf
removes a non-empty directory
cp -r ‘dir1’ ‘dir2’
copy dir1 into dir2 including subdirectories
pwd
print working directory
cd ~
changes to home directory
whoami
returns your username
sudo command
execute command as the root user
lsof
lists all open files
varname=value
defines a variable
echo $varname
checks a variables value
read -p “Please enter your name: “ name
prompts the user for input with a specified message and stores the entered value in a variable
export -f ‘f1’
export function called f1
How do you define an array
array[0]=valA
How do you display the array’s value for a certain index
${array[i]}
what do you put at the beginning of your bash script
!/bin/bash
what are some arguments for chmod
–recursive - changes files and directories recursively
–changes - report when a change is made
–silent - suppress most error messages
what does apropos keyword mean?
Its when you don’t know the command name but roughly know what you want to do.
man 3 printf
this will open up the third man page of the prinf command
what does rm file* do?
removes files 1 to infinity that have digits after the filename. Ex: file1 file2
rm file?
removes files with one character after filename. files{0-9}
*(abc)
would match “”, “abc”, “abcabc”, “abcabcabc”, etc.
+(abc)
would match “abc”, “abcabc”, “abcabcabc”, but not an empty string “”
?(abc)
would match “” or “abc”.
@(abc|def)
would match “abc” or “def”, but not both or neither
!(abc)
would match any string that is not “abc”
what is the AND operator and the OR operator
&& - AND || - OR
str1 == str2
the strings match each other
what are the terms for less than, greater than or equal to, not equal, etc..
-lt # less than
-le # less than or equal
-eq # equal
-ge # greater than or equal
-gt # greater than
-ne # not equal
what is the criteria in starting and stopping a for loop
do and done
what are the requirements in an if statement
if condition
then
statements
[elif condition
then statements…]
[else
statements]
fi
how do you do an incremental for loop in bash
for (( initialisation ; ending condition ; update ))
what does the pipe function do for this example cmd1 | cmd2
This takes the results from cmd1 and sends them to cmd2
what happens here? ls | grep “.txt”
ls lists all the files, and grep “.txt” looks through that list to find only the text files.
history
show command history
source /file
runs every command one line at a time
!!
repeats the last command
echo $SHELL
displays the shell you are using
echo $PATH
shows the entire path you are in
whereis bash
shows location, source code and path
ls
list files in directory
env
displays environment variables
what outputs when ls -l is printed to the console
displays
type of file
type of permission access
ACL flag
Links
Owner
Group
Size
Date and Time
Filename
ls - a
displays hidden files
ln -s ‘filename’ ‘link’
creates link to a file
readlink ‘filename’
shows where the link points to
tree
shows directories in the tree format
touch file
creates or updates your file
cat file
displays raw contents of file
cat -n file
shows numbers of lines
nl file
shows number of lines in file
cat f1 > f2
copy f1 to f2
cat f1»_space; f2
merge 2 file texts together
echo “New entry” > file2
directs and overwrites New entry to file2
file1»_space; file2
directions and appends contents of file1 to file2
more file1
shows the first portion of the file1
head file1
shows the first 10 lines of file1
tail file1
shows the last 10 lines of file1
What does filename=$(basename “$file” .txt) accomplish?
removes the .txt extension from the filename and stores the result in the variable filename
what does mkdir -p /path/to/directory
If /path/to/ does not exist, it will create both path, to, and directory. If directory already exists, it won’t raise an error.
When calling a variable in your bash script that has already been initialized, what 2 things must you always include? Lets call the variable filename
for “$filename” in …..
You must always do what 2 things when writing an if/elif/else statement in bash?
Use []; if [ “$file” -gt 6];
What does ./mean < “$input_file” > “$output_file” do?
executes the mean program, taking input from input_file and saving the output to output_file
what does echo “hello” | tr “aeiou” “12345”
translates the vowels a, e, i, o, u in the string “hello” into the numbers 1, 2, 3, 4, and 5. The output would be h2ll4
what is an example of a file you are looking for when you say grep -r “multiply(.,.,.*)”
multiply(1, 2, 3)
what would this function be looking for? grep “multiply(.,.,.*)” math
And what we be in the contents?
example_math
Contents:
multiply(1,2,3)
What does ‘s/[0-9]kg,/&&/g’ do?
finds occurrences of a digit followed by “kg,” and a comma in a text and replaces them with the same matched content
What happens if you run multiple commands with the > operator sequentially on the same file?
The contents of the file will be overwritten by the output of the last command executed.
What does the command command 2> error_log do?
runs command and redirects any error messages (stderr) to a file named error_log
How can you redirect both standard output and standard error to a single file?
command > output_file 2>&1
what does this function do, ls | tee directory.txt | sort
Lists the contents of the current directory, saves that list to a file called directory.txt, and displays the sorted output in the terminal.
what does, cat file | command do?
reads the contents of file and pipes that content into command for further processing
How does the uniq command work?
uniq filters out repeated lines in a sorted file, displaying only unique lines or the number of occurrences.
what will the results of this input be? seq 2 2 20
2
4
6
8
10
12
14
16
18
20
What is the purpose of the awk function?
It helps you look at specific sets of data and do things with that information. You can tell it what to find and what to do with it.
If your file contains:
Alice 88
Bob 92
Charlie 67
Diana 85
Eve 95
What will this function produce?
awk ‘$2 > 90 { print $1 }’ grades.txt
Bob
Eve
If your file contains:
Alice 88
Bob 92
Charlie 67
Diana 85
Eve 95
What will this prompt produce?
awk ‘{
if ($2 > max) {
max = $2;
name = $1;
}
} END { print “Top scorer:”, name, “with a score of”, max }’ grades.txt
Top scorer: Eve with a score of 95
what does this while loop do?
count=1
while [ $count -le 5 ]; do
echo “Count $count”
((count++))
done
Count 1
Count 2
Count 3
Count 4
Count 5
What does the command wc file.txt do?
counts the number of lines, words, and characters in the file file.txt, displaying the results in three columns:
What does the command basename /home/user/documents/report.txt return?
report.txt
what does chmod -R do?
Apply the permission change recursively to all the files and directories within the specified directory.
what does chmod -c do?
It will display a message for each file that is processed. while indicating the permission change that was made.
what does chmod -f do?
It helps in avoiding display of error messages.
what does chmod -h do?
Change the permissions of symbolic links instead of the files they point to
What does the -c option do in tar?
It creates an archive by bundling files and directories together.
What does the -x option do in tar?
It extracts files and directories from an existing archive.
What does the -f option do in tar
It specifies the filename of the archive to be created or extracted.
What does the -v option do in tar?
It shows extra details about what’s happening while files are being added to or taken out of the archive
What does the -r option do in tar?
It updates or adds files or directories to an already existing archive without recreating the entire archive.
what numbers represent the rwx commands?
A 1 gives execute permission, a 2
gives write permission, and a 4
gives read permission
what does the chown command do?
Change owner of file/dir
(admin only)
what does chgrp do?
Change group of a file/dir
What’s an ACL?
ACLs provide more granular control over file access than traditional permissions, but may not be necessary for all systems.