Linux Sysadmin Test Prep

File Manipulation

File Manipulation

sed

sed 's/and/\&/g;s/^I/You/g' example.txt (multiple search and replace global)

sed -n '/^Oct 3/ p' /var/log/syslog | sed -n 1,5p (print only lines 1-5 starting with "Oct 3")

sed '/junk/d' file.txt (delete "junk" lines)

sed 'G' file.txt (add new line after each line)

uniq, sort, cut, tr

cat /var/log/syslog | uniq -c -w 6 (count, 6 wide [date space])

du -sch /var/* | sort -h ( file usage - summarize, total, human readable | sort human numerical )

cat file.txt | tr [:lower:] [:upper:]

last | tr -s ' ' | cut -d' ' -f1,3 | sort -k2 | uniq


Complete each exercise in a single line of code.

  1. There are files we won't want mixed in with files we do want. Search all the folders and delete all the files with "junk" in their filename (case insensitive).

  2. Then, delete everything in /danielsTestDrills/answers/ except for files containing "answer" (case insensitive).


  1. Copy lines 1, 2, 3, and 9 from /danielsTestDrills/testFile-1 into a new file /danielsTestDrills/testFile-2.

  2. Copy all lines from /danielsTestDrills/testFile-1 with with any data between "daniel" and "pass the LFCS" (case insensitive) and append into /danielsTestDrills/testFile-2.

  3. Copy any lines with the word "stuff" that does not come at the beginning or end (ignoring trailing whitespace) of a line from /danielsTestDrills/testFile-1 and append into /danielsTestDrills/testFile-2.


  1. Copy any lines with ! from /danielsTestDrills/testFile-1 into a new file named /danielsTestDrills/testFile-3.

  2. Copy all lines with ordinal numbers (1st - 10th) from /danielsTestDrills/testFile-1 into the file /danielsTestDrills/testFile-3.

  3. Copy any lines starting with daniel (case insensitive)from /danielsTestDrills/testFile-1 into the file named /danielsTestDrills/testFile-3.

  4. Move the first line of /danielsTestDrills/testFile-3 to the end of the file.


  1. Delete any lines from /danielsTestDrills/testFile-1 with the "junk" or "Junk" in them.

  2. Delete line 4 from /danielsTestDrills/testFile-1.

  3. Delete any lines starting or ending with "stuff" from /danielsTestDrills/testFile-1.


  1. Compare the three files in /danielsTestDrills and put the file name of the different file into /danielsTestDrills/answers/answerFile. Do not include leading path information.

  2. Find the single unique line and append it to /danielsTestDrills/answers/answerFile

Make a tar archive of the three testFile files and save it as/danielsTestDrills/answers/testFiles.tar and then remove the three files.

add ex gc ask about changes

Edit this page on GitHub