Basic Linux commands:
1. Below are the list of commands that can be run with no options & understand why & where to use it:
cd – change directory
ls – list contents of a directory
rm – remove file or empty directory
cp – copy file or directory to file or directory
mv – move file or directory to file or directory
mkdir – make directory
pwd – display present working directory
touch – touch a file to either create it with no content or update it’s last accessed time
date – displays the current date
hostname – display computer’s host-name
id – display user’s identity information
echo – echo argument to output
head – print first ten lines of a file to output
tail – print the last ten lines of a file to output
alias – display aliases configured for user
set – display all shell variables for user
env – display exported shell variables for user – these are inherited from a parent process
which – display which command from the folders in the path variable would run
type – display which command will run and what type of command that is (Ex: type ssh —> ssh is /usr/bin/ssh)
vi – To edit the file
2. Use input-output redirection (>, >>, |, 2>, etc.)
Three standard streams of information:
stdin – (Standard Input)
stdout – (Standard Output)
stderr – (Standard Error)
> – Redirect output. Example:
cat file1 file2 > file3
contents of file1 and file2 will replace the contents of file3 if file3 already exists.
>> – Redirect output appending existing data. Example:
cat file1 file2 >> file3
contents of file1 and file2 will be appended to the contents of file3.
2> – Redirect standard error. Example:
cat file1 file2 > file3 2> file4
contents of file1 and file2 are sent to file3 and any error messages are sent to file4.
&> – This is the same as 2>.
< – Direct alternative input. Example:
mail person < info_file
contents of info_file is used as input for the mail command so that person is mailed the contents of info_file.
” | “- Pipes output from a command/program to the input of another command/program. Example:
cat file1 file2 | grep argument | wc -l
contents of file1 and file2 are piped to the ‘grep‘ command which finds all lines containing ‘argument’ and those lines are piped to the wc -l command which counts how many lines were found.
Another example is:
ls -a | sort -i
the output of ls -a is piped to the sort -i command which displays that output sorted ignoring any dots at the start of hidden files. Another example:
cat file1 file2 | grep argument | sort -i
xargs – Run a command multiple times using a list of arguments. Usage syntax:
xargs [options] command [initial arguments]
find /root -type f -print | xargs grep -n “text string”
the find command looks in /root for regular files using the ‘-type f’ option and prints each file name using the ‘-print’ option but the output is piped to xargs and that passes each file name to grep one at a time, grep then searches each file for “text string”.
tee – Redirect output to a file while also allowing that output to continue to the display (standard output). Example:
ls -a | sort -i | tee filelist
the ls -a command makes a list of all the files and directories within the present working directory and that is piped to the sort -i command. The sorted list in then piped to the tee command which saves the list to a file named filelist and also displays the list on screen
3. Use grep and regular expressions to analyze text
The ‘grep‘ command is used to search for files using “filename expansion”, or to search for character patterns using “regular expressions”.
The syntax is: grep options expression filename
Filename expansion uses metacharacters to abbreviate filenames. When using filename expansion, metacharacters expand into filenames.
Regular Expressions define character sets or strings to facilitate pattern matching.
Metacharacters allow the use of grep to search within a file defined with an abbreviation of its filename.
* – Matches zero or more characters (can be an entire filename)
? – Matches one character
[ ] – Square brackets are used to match their contents in the following way:
grep “expression” [abc]*
files with names starting with either ‘a’, ‘b’ or ‘c’ are searched with grep using the “expression”. This can also be achieved by defining a range so that files with names starting with ‘a’ through to ‘c’ are searched:
grep “expression” [a-c]*
To exclude characters, the caret symbol is used within the brackets in this way:
grep “expression” [^abc]*
all files within the PWD with names that do not start with ‘a’, ‘b’ or ‘c’ will be searched.
Search strings can be defined within the brackets using commas to separate the strings:
grep “expression” [a*,b*c*]
this will search all files with names that start with an ‘a’ and also all files with names that start with a ‘b’ and contain a ‘c’.