Command Challenge¶
- List names of all the files in the current directory, one file per line.
ls -1A
# -1 - list one file per line
# -A, --almost-all - do not list implied . and ..
for f in *; do echo $f; done
- Print the last 5 lines of "access.log".
sed -n '6,10p' access.log # will print lines 6 through 10 from the `access.log` file to the terminal.
awk -v start_line=$(( $(wc -l < llama.log) - 4 )) 'NR >= start_line' llama.log
tail -n 5 access.log
- Delete all of the files in this challenge directory including all subdirectories and their contents.
[!hint] There are files and directories that start with a dot ".", "rm -rf *" won't work here!
- Print all files in the current directory, one per line (not the path, just the filename) that contain the string "500".
grep -lr 500
# -r, --recursive - Read all files under each directory, recursively, following symbolic links only if they are on the command line.
# -l, --files-with-matches - Suppress normal output; instead print the name of each input file from which output would normally have been printed. Scanning each input file stops upon first match.
- Print all matching lines (without the filename or the file path) in all files under the current directory that start with "access.log" that contain the string "500".
[!note] -h, --no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
- Extract all IP addresses from files that start with "access.log" printing one IP address per line.
grep -r . | awk '{print $1}'
cat **/access.log* | awk '{print $1}'
awk '{print $1}' **/access.log*
grep -ro ^[*-9]* # -o, --only-matching
- Count the number of files in the current working directory. Print the number of files as a single integer.
- Print the contents of access.log sorted.
- Print the numbers 1 to 100 separated by spaces.