01_Linux Basic Commands
01_Linux Basic Commands
1
Linux Basic commands
File system commands
● mkdir – make directory
#mkdir <dirname> # mkdir -p path/test/test1
-p > make parent directories as needed
● cd -change directory
Type mv followed by the current name of a directory and #mv testdir newdirname
the new name of the directory.
● mount - Displays all mounted devices, their mount point, filesystem, and access.
2
Shell Metacharacters
These are special characters that are recognized by the shell
[] - This will match any single character in the range #ls tut[09].m
This will find files such as tut0.m, tut9.m etc.,
| - This is a pipe character. Sends the output of first command as input #who | grep sam
for the second command
pwd - print working directory will show you the full path #pwd
to the directory you are currently in.
link - Creates a symbolic link named symlink that points #ln - s test symlink
to the file test
3
du - summarize disk usage of each file, recursively for #du -h
directories.
find - Find locations of files/directories quickly across #find / -name appname -type d -xdev
entire filesystem
-type d --search for the directory named appname
-xdev -- Don't descend directories on other filesystems.
-search -- against all directories below / for the appname
found in directories but only on the existing filesystem.
find - Command to find and remove files #find . -name "FILETOFIND" -exec rm
-rf {} \;
more - commands are used to view large files one page at a #more <file name>
time
less - commands are used to view large files one page at a time #less <file name>
wc - command is used to count lines, words and characters, #wc [options] [file name]
depending on the option used.
4
You can just print number of lines, number of words or number
of characters by using following options:
-l : Number of lines
-w : Number of words
-c : Number of characters
Filters
Filters are commands which accept data from standard input, manipulate it and write the
results to standard output.
head - displays the lines at the top of the file #head filename
when used without any option it will display first 10 lines of the #head -n 10 filename
file
-n > print the first N lines instead of the first 10
tail - displays the lines at the end of the file. By default it will #tail filename
display last 10 lines of the file
paste - command will paste the contents of the file side by side #paste a.txt b.txt
Pattern Searching
grep -scans its input for a pattern, displays the line containing #grep options pattern filename(s)
that pattern
grep - searching for a text string in multiple files #grep ‘root’ *.txt
grep - Reversing the meaning of a grep search. #grep -v ‘boss’ /etc/passwd
Displays all the lines that do not contain the specified pattern
egrep with pipeline - Linux grep command to search for #egrep ‘boss|root’ /etc/passwd
multiple patterns at one time
5
grep - pattern matching and regular expressions (regex #grep '[FG]oo' *
patterns) #grep '[0-9][0-9][0-9]' *
#grep '^fred' /etc/passwd
Task Automation
Cron is the name of a program that enables linux users to execute commands or scripts
(groups of commands) automatically at a specified time/date.
You can set up commands or scripts, which will repeatedly run at a set time.
● The cron service (daemon) runs in the background and constantly checks the
/etc/crontab file, /etc/cron.*/ directories.
● It also checks the /var/spool/cron/ directory.
crontab - To edit the crontab file, type the following #crontab -e
command at the Linux shell prompt:
Syntax of crontab (Field Description) m h dom mon dow
where /path/to/command arg1 arg2
m: Minute (0 - 59)
h: Hours (0- 23)
dom: Date (0 - 31)
mon: Month (0 - 12 [12 == December])
dow: week days(0- 7 [0 or 7 sunday])
/path/to/command - Script or command name to schedule