Bash Script

BASIC COMMANDS:

  • ls lists all files
  • ls -al lists a long list containing several information for file
  • man -manual for a command..eg man ls
  • ctrl+x exit from file or to stop a command
  • ctrl+c kills the current process
  • cd / -go to the root directory
  • cd ~ go to the user directory
  • cd - go to the previous directory
  • UP and DOWN arrow keys can help navigate through previously used commands
  • history command gives us the list of previously used commands
  • grep command can be piped with various other commands using “|” :: eg
history | grep git , find / -name "folder_name" 2>/dev/null | grep $USER

In order to change directory, we can use absolute or relative paths for acessing directories

  • cd directories path: moves to the respective directory
  • cd .. move to the previous directory

FILE HANDLING

  • touch: Creating a new file. eg touch a.cpp
  • mv prev new: Renaming a new file that is move the file to new file
  • rm file name: Permanently deleting a file
  • cat filename: Viewing the contents of a file
  • cat prev_file > new_file : creates a new file with file name new_file and with content as that of prev_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 empty
  • rm -ir dir_name : helps to delete first the contents in the directory and then the directory itself recursively
  • find / -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 suffixes
  • grep 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