1 - What on earth is Linux?
Unit 1 - Introduction and Basics
Last Update Unknown
Relative and Absolute Paths
On its own it changes it to your HOME directory.
This is an absolute path and takes you to the path specified (in this case dir1) from your root directory.
This is a relative path which takes you to dir1 from your current working directory.
Pipe
In Linux, the pipe command lets you sends the output of one command to another.
Piping can redirect the standard output, input, or error of one process to another for further
processing.
The syntax for the pipe is the | character between any two commands:
For example, if I wanted to get the 100th filename in a directory, I would use the command:
- 'ls' gets all the files in the directory
- 'head' gets the first 100 of those files
- 'tail' takes the last one of the first 100 files
Example Question
Using a combination of grep, regular expressions, and wc via a pipe count how many words in the /usr/share/dict/words dictionary starts with "anti" and ends with an "n".
The caret symbol (^) can be used at the start of a regular expression to indicate that a match must occur at the beginning of the searched text.
The dollar symbol ($) matches the end of a string and would only return a match when the text or string it is
attached to is at the end of a line.
Example Question 2
Using grep and regular expressions, find all the words which start with "tele" from /usr/share/dict/words,
and which are exactly 7 characters long.
egrep is the same as grep -E and allows for the use of extended regex in your standard grep searches!
Redirect
To redirect a standard output of any command to another process or a file/stream, use the > symbol. To redirect a standard input of any command, use the < symbol.
For example, if I wanted to store the names of all the files in a directory in a text file, I would use the command:
The redirection operator is also used to append to a file.
You can use the >> operator to add lines of text to the end of a file.
For example, if I want to append the month's current calendar to a file, I would use the command:
Signatures
The file command can be used to determine the type of a file.
The md5sum command is used to print or check MD5 (128-bit) checksum.
If another file has the same md5 hash, this means that the file content is probably identical.
The cmp command compares two files byte by byte and can be used to verify whether files are identical.
Find
The find command helps you find things, and not just by filename.
Find a single file by name
When you know the name of a file but can't remember where you saved it, use find to search your home directory.
Use 2>/dev/null to silence permission errors (or use sudo to gain all permissions).
Find a single file by approximate name
To do a partial and case-insensitive search:
Find everything
The ls -R command lists the contents of a directory recursively, meaning that it doesn't just list the
target you provide for it, but also descends into every subdirectory within that target (and every
subdirectory in each subdirectory, and so on.) The find command has that function too, using the -ls
option:
Find by content
To search for a file by content rather than by name:
Find files by type
You can display files, directories, symlinks, named pipes, sockets, and more using the -type
option.
And more...
GREP and regexp
In Linux, the grep command can be used to find or search a regular expression or a string in a text file.
To search for a string in a file, use the syntax below:
For example, if I wanted to find the word 'Linux' in a file 'welcome.txt', I would use the command:
You can further search for a string in your current directory and all other subdirectories, using the -r flag:
To ignore case sensitivity, we use the command:
We can also count the number of lines where strings are matched using the -c option:
You can also invert the Grep output, using the -v flag:
To search for an exact word, use the -w option: