# Linux Commands ## Useful Links | Descriptoin |Link | | -------- | -------- | | Crontab Guru|https://crontab.guru/| | Example Bash|https://github.com/alexanderepstein/Bash-Snippets/blob/master/todo/todo| |Popular Bash Aliases|https://www.digitalocean.com/community/questions/what-are-your-favorite-bash-aliases| |Regex|https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url/3809435#3809435 https://regexone.com/ |List Of Countries|https://gist.github.com/kalinchernev/486393efcca01623b18d| |OS Family Tree| https://eylenburg.github.io/os_familytree.htm| ## Folder Structure ``` \ => This is the root directory - bin => Binaries. Executable programs. - home => - jayaraj - Desktop - Pictures - Downloads - Documents - Music - priya - sarang - root => home directory for the super user root. - dev - usr => - etc => Configuration files. - media => removable media. CD ROM etc. - var => Logging, cache etc ``` ## Permissions ### Understanding | Permissions| Owner | Group Owner | Size | Last Modified | File Name| | --------| -------- | -------- | -------- | --------| --------| |drwxr-xr-x 2| ricky |ponting | 92K |May 3 2021 |Cases| |-rw-r--r-- 1| ricky |ponting | 12M |Mar 11 14:52 |02_Command_Basics.pdf| |-rw-r--r-- 1| ricky |ponting |2.1M |Mar 11 14:52 |01_Linux_Introduction.pdf| |-rw-r--r-- 1| ricky |ponting |2.3M |Mar 11 14:54 |03_Getting_Help.pdf| |-rw-r--r-- 1| ricky |ponting |2.5M |Mar 11 22:10 |05_Creating_Files_And_Folders.pdf| |-rw-r--r-- 1| ricky |ponting |5.7M |Mar 11 22:20 |04_Navigation.pdf| |-rw-r--r-- 1| ricky |ponting |864K |Mar 12 14:29 |06_Nano.pdf| | Attributes | Description| Comments | | -------- | -------- | -------- | | - | regular file | | | b | Block Files | Associated with a device | | c | Character | Associated with a device | | l | Symlink | Symbolic Link | | d | directory | | > 10 Char Permission Set > r - Read > w - Write > x - Execute > - - Cannot read write or execute. | x |xxx| xxx |xxx| | -------- | -------- | -------- | -------- | | Type: File/Folder | Owner's Permissions | Group's Permissions | World | | d | rwx | r-x| r- - | | - | rwx | r-x| r- - | ### Manipulating `chmod` # Changing permission chmod mode file **Who** u - user g - group o - others (World) a - everyone (All the above) **What** - removes permission + grants permission = set a permission and remove others **Which** r - read w - write x - execute `su` # substitute user. `chown` # change owner or group owner of a file or directory `sudo chown <user> <file/dir>` `sudo` # run commands as the root user. `addgroup` `adduser` ## LINUX Commands ## Environment #print environment vars `printenv` #Parameter expansion Example: `printenv | grep HOME` ` echo $USERNAME` Example: ` echo $PWD or echo $PATH` #Define custom shell variable `variable =value // Shell variables are local. Only exists in current shell session` #Define custom environment variable `export <variablename> // export CONNECTIONSTRING=http://api.intetrnal.com` #Startup files `/etc/profile` - all users global profile `~/etc/bash.bashrc` - Non login related . Global config for all users. `~/.bashrc` - Each specific users home directory. We customize this for aliases, custom prompts etc for the given user. #aliasing `alias pig=echo OINK` # whenever you type pig OINK will be output `type pig` #will show what the alias is about. #Define the above in ~/.bashrc to make it always available. ## Date Time ``` date # prints current date/time ## Note that the arguments are positional (Order does matter) cal # horizontal calendar cal july 1969 ncal #vertical calendar with current day selected ncal july 1969 ncal 1999 ``` echo # echos what you type after the commands sort <filename> # sorts and prints the contents of the file ## Accessing Manual pages `man <command>` Press q to exit f to forward by one page b to backward by one page up/down arrows to line by line SPACE forward by one window/page / -- use to search man pages ### Searching Manual Example : man -k dog # searches for section that contains dog type <command> # Prints the type of command For shell built-ins you may not find man pages. In that scenario use `help <commandname>` `ls` list contents of the directory `history` `history | less` => History in pages `Ctrl + R ` search history. `head` Show first 10 lines `tail` Show last 10 lines `tail -n 4` Specific # of lines `tail -f <filename>` ### Redirecting Numeric file descriptor for Standar Streams stdin 0 stdout 1 stderr 2 Examples `ls 1> list.txt` `ls zzz 2> error.txt` Redirecting stdout and stderror n single statement `cat bees.txt ants.txt > insects.txt 2>> error.log` Redirecting stdout and stderror to the same destination `ls > output.txt >2 output.txt` OR `ls > output.txt 2>&1` (latter is just a syntactic sugar) ### Piping Examples `ls -l /usr/bin | less` # Pass the output of ls -l to less `date | rev` # Pass output of date to rev command `ls /usr/bin -1 | wc -l` # count the number of files. `ls /usr/bin -lh | sort -k5h |tail -3` # List three largest files in a given folder. `du -ha /usr/bin/ | sort -h | tail -3` # Same as above. Better ### Expansions Pathname expansion `echo *` Wild Card : ? `ls *.???` # list all files that has three char extensions. Range Wild Card: `ls app[12].css` #Result: app1.css app2.css `ls [a-z]*` # Result: files that begins with lowercase alphabet filename. `ls [^a]*` # Result file list that does not begin with lower case a ### Finding files `locate` # uses a database. Requires a search pattern `find` # slower. But more powerful and realtime # find with no arguments will list every single file in the directory and sub directories. `find /` # willl start listing every single file in the system `find -type f` # find only files `find -type d` # find only directories `find ~/Desktop -name *.txt` # find pattern based `find ~ -type f -name "*.txt"` `find ~ -type f -iname "*std*"` # find with case insensitive name search. `find ~/Desktop -type f -name "*[0-9]*"` # any file that contains a number in it's name. `find / -type f -size +1G` # find files that are bigger than 1G in size` `find / -user ponting` # find file that are owned by certain users `find ~/Desktop -empty -type d` # find empty folders `find -mmin +30` # find files modified 30 minutes ago. `find -mmin -30` # modified in 30 minutes `find -mmin 30` # modified exactly 30 minutes ago `find -type f -not -name "*.pdf"` # Find files that are not .pdf `find -empty -exec ls -l '{}' ';' # ls ### Timestamp mtime # modificationtime ex: ` ls -l` ctime # change time. Rename move etc.` ls -lm` atime # Last access time. ` ls -lu` ### Grep : Search inside files || text `grep "chicken" animals.txt` `grep bequeath SongOfMyself.txt` `grep -w "I" SongOfMyself.txt` # search for I. But will include It as well. `grep -w "I" SongOfMyself.txt -i` # Case insensitive search `grep -w "I" SongOfMyself.txt` # Word search `grep -r "9198027949" ~/Desktop` # Find across files ina particular folder / recursively ` grep "myself" SongOfMyself.txt -ic` # How many times myeslf is used `grep "I" SongOfMyself.txt -iwc` # How many times I is used as a word `grep "wagon" SongOfMyself.txt -A2` #include two lines after `grep "wagon" SongOfMyself.txt -B2` #include two lines before `grep "grass" SongOfMyself.txt -wn` # include line number as well `grep "grass" SongOfMyself.txt -nC2` # show grass , 2 lines before and after and include line numbers. ### Regular Expressions `. matches any single chcaracter` `^ matches the start of a line` '$ matches the end of a line' '[abc] matches any character in the set' '^[abc] matches any character NOT in the set' '[A-Z] matches any characters in a range' '* Repeat previous expression` `\ Escape meta characters` ## Cron Jobs `crontab -e`