Common tasks/problems

Note: Some of these questions were taken from the BashFAQ at https://mywiki.wooledge.org/BashFAQ, or are covered in the FAQ. Where I found related FAQs, the number is given after the question. # is the comment character in . Things after it are ignored by the shell. Documentation, Interactive use 1. How do you get help on the command line, how do you search inside a manual (man,info,help, search: / ) man command # man pages, most common case info command # info pages. If they exist, they often have more info than man pages help command # this is only for bash internal commands (also documented in „man bash“) use / to search for things in man pages, „man bash“ has ~ 6000 lines! 2. What are three ways to find lines in your command history? (history) history (command), pressing ctrl-R (searches backwards) and arrow upwards (last command) Quoting, Variables 3. What is the result?

boo=bla; echo $boo , "$boo" , '$boo' the shell (not echo!) will expand the $boo if it is not quoted. ""-quotes still expand variables, but quote all other things special to the shell. '' will prevent variable expansion. 4. How can you put the output of a command into a variable? Put the output of ls -l into the variable „foo“. (bash, FAQ2) foo=$(ls -l) # $() is command substitution. It executes the command in a subshell and replaces the command substitution with the output of the command. Note quoting again. echo "$foo" and echo $foo return very different things! 5. Set a variable with foo="bla blu bli" . How can you remove the leading „bla “ and echo the result? echo ${foo#bla } # this is a shell internal way to manipulate variables echo $foo | sed 's/^bla //' # this would use an external command and is slower 6. Use the command echo to print out the following strings: (bash quoting, https://mywiki.wooledge.org/Quotes covers quoting extensively) a) it's a weird thing echo "it's a weird thing“ # you can escape single quotes using double quotes b) he said: "it's man's biggest fault" echo 'he said: "it'\''s man'\''s biggest fault"' #there is no elegant/nice way echo he said: \"it\'s man\'s biggest fault\" File manipulation 7. How can you find the biggest 5 files in your current directory? How can you find the 5 biggest files and directories? (ls, du, pipes, sort, tail) ls -lhrS | tail -5 # the last 5ish files. Filenames can contain newlines. Parsing ls output is not safe. du -hs * | sort -h | tail -5 # same. Also ignores dot-files du -hs * .[^.]* | sort -h | tail -5 # with dot-files. „.[^.]*“, because we want to ignore „..“ 8. Your calculation produced a logfile mycalc.log. a) Use a command to print all lines in which the second column contains the string „Energy:“ ; b) You notice you are only interested in column 4 and 7 of those lines. What command do you use now? (awk) awk '$2~/Energy:/{print}' mycalc.log awk '$2~/Energy:/{print $4 " " $7 }' mycalc.log 9. You are looking for a file somewhere in your home directory. You only remember that the file name contained the word "humbug". How do you search for that file? (find) find $HOME -type f -name \*humbug\* find $HOME | -i humbug # this is case insensitive. Again not safe with filenames containing newlines. 10.How can you rename all files in a directory called output-XXX.log to old_output- XXX.log (where XXX varies)? (rename,prename, more generic: bash loop, bash variable substitution, FAQ 30) rename and prename are/should be the same. On some systems rename is some different program. In this case get prename and install it in your $HOME or use the other method. prename is a script and uses a perl substitution pattern. prename 's/^/old_/' output-*log for file in output-*log; do mv "$file" old_"$file"; done Text Files 11.You are looking for a file somewhere in your home directory. You don't remember anything about the filename, but the file should be a text file containing the line „here we are“. How do you search for that file? (find, grep, quoting, FAQ20) find . -type f -exec grep -il "here we are" {} + 12.You have a text file containing 500 lines of comments (lines starting with „#“) and 5 lines of code, that you want to see. How do you display only those lines? (sed) sed '/^#/d' textfile sed '/^[ ]*#/d' textfile # also ignore lines with spaces before the comment 13.You have a file $HOME/somedir/bla. You want anyone on the system to be able to read that file. What permissions do you have to set (or check if they are already set)? (chmod, FAQ38) chmod a+x $HOME $HOME/somedir # all directories must be „executable“ for a user to even be able to get to the directory the file is in chmod a+rx $HOME/somedir/bla

14.You only want to allow a single user to read that file, how do you do that (setfacl)?

man setfacl # :P

setfacl -m username:x $HOME somedir

setfacl -m username:rx $HOME/somedir/bla 15. How can you redirect all errors / all normal (non-error) output / all output to a file? (pipes, FAQ47) ls > file # „stdout“ normal output into file, errors printed on the screen ls 2> file # errors go to the file, output printed ls > file 2>&1 # both go to the file. 16. How can you watch the changes to a growing log file? (tail, FAQ49) tail -f logfile Processes 17. How do you find the process ID of a running process or of a process using much of the CPU power? (ps|grep, top, htop, FAQ33) ps aux | grep process_name pgrep process_name top htop

18. How can I find out if a process is still running? (FAQ42) kill -0 PID # there is no signal -0, but you will get an error if the process doesn't exist only on : check if /proc/PID exists