Bash Script
BASIC COMMANDS:
ls
lists all filesls -al
lists a long list containing several information for fileman
-manual for a command..eg man lsctrl+x
exit from file or to stop a commandctrl+c
kills the current processcd /
-go to the root directorycd ~
go to the user directorycd -
go to the previous directoryUP
andDOWN
arrow keys can help navigate through previously used commandshistory
command gives us the list of previously used commandsgrep
command can be piped with various other commands using “|” :: eg
history | grep git , find / -name "folder_name" 2>/dev/null | grep $USER
NAVIGATION:
In order to change directory, we can use absolute or relative paths for acessing directories
cd directories path
: moves to the respective directorycd ..
move to the previous directory
FILE HANDLING
touch
: Creating a new file. eg touch a.cppmv prev new
: Renaming a new file that is move the file to new filerm file name
: Permanently deleting a filecat filename
: Viewing the contents of a filecat prev_file > new_file
: creates a new file with file namenew_file
and with content as that ofprev_file
nano file_name
: helps creates a new_file with name " file_name" and to edit it
FINDING INFORMATION
mkdir dir_name
: creates a folder in the respective directory with name “dir_name”mv dir1 dir2
: moves folder namrd “dir1” to folder named “dir2”rmdir dir_name
: deletes a directly only if its emptyrm -ir dir_name
: helps to delete first the contents in the directory and then the directory itself recursivelyfind / -name "folder_name"
: if called from the root directory,it searches all the files and folders to get folder named “folder_name”find / -name "folder_name" 2>/dev/null
: helps search for folder named “folder_name” by skipping all the error messages We can use*
in file or folders name while looking for it if we are not sure of the prefixes or suffixesgrep name
: this command can help search through the contents of the file using regular expressions. It searches for “name” in all the contents
ALIASES
They are shortcut for long codes. We can create our own aliases in any file named for eg.my_aliases
..then we have to write the code in it in proper syntax. After that we will have to run the source command for shell to recognize our file and then use our shortcuts :)
TAKING INPUT AND WRITING OUTPUT
./a.out < input.txt > output.txt