{"id":19957026,"url":"https://github.com/anmolnagpal/bash-commands","last_synced_at":"2025-05-03T21:30:28.187Z","repository":{"id":94618939,"uuid":"138488545","full_name":"anmolnagpal/bash-commands","owner":"anmolnagpal","description":"Bash commands for daily use ","archived":false,"fork":false,"pushed_at":"2018-06-30T18:42:26.000Z","size":24,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-07T21:12:15.303Z","etag":null,"topics":["bash","command","command-line","devops","linux","macos"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anmolnagpal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-24T14:27:14.000Z","updated_at":"2020-05-22T10:04:01.000Z","dependencies_parsed_at":"2023-04-26T14:00:31.188Z","dependency_job_id":null,"html_url":"https://github.com/anmolnagpal/bash-commands","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anmolnagpal%2Fbash-commands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anmolnagpal%2Fbash-commands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anmolnagpal%2Fbash-commands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anmolnagpal%2Fbash-commands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anmolnagpal","download_url":"https://codeload.github.com/anmolnagpal/bash-commands/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252259295,"owners_count":21719658,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","command","command-line","devops","linux","macos"],"created_at":"2024-11-13T01:36:21.742Z","updated_at":"2025-05-03T21:30:28.174Z","avatar_url":"https://github.com/anmolnagpal.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003cimg src=\"https://user-images.githubusercontent.com/4303310/42126873-cbb93eea-7c9f-11e8-893a-7d20ff59d60e.png\" /\u003e\u003c/p\u003e\n\n\u003e Learning command line tools for managing the file system in Bash or alternative shells like, Zsh, can give you more speed and control over your workflow. \n\n### ✒ Commands\n##### How to display the 10th line of a file?\n```bash\nhead -10 filename | tail -1\n```\n##### How to remove the header from a file?\n```bash\nsed -i '1 d' filename\n```\n##### How to remove the footer from a file?\n```bash\nsed -i '$ d' filename\n```\n##### Write a command to find the length of a line in a file?\n\nThe below command can be used to get a line from a file.\n```bash\nsed –n '\u003cn\u003e p' filename\n```\nWe will see how to find the length of 10th line in a file\n```bash\nsed -n '10 p' filename|wc -c\n```\n##### How to get the nth word of a line in Unix?\n```bash\ncut –f\u003cn\u003e -d' '\n```\n##### How to reverse a string in unix?\n```bash\necho \"java\" | rev\n```\n##### How to get the last word from a line in Unix file?\n```bash\necho \"unix is good\" | rev | cut -f1 -d' ' | rev\n```\n##### How to replace the n-th line in a file with a new line in Unix?\n```bash\nsed -i'' '10 d' filename       # d stands for delete\n\nsed -i'' '10 i new inserted line' filename     # i stands for insert\n```\n##### How to check if the last command was successful in Unix?\n```bash\necho $?\n```\n##### Write command to list all the links from a directory?\n```bash\nls -lrt | grep \"^l\"\n```\n##### How will you find which operating system your system is running on in UNIX?\n```bash\nuname -a\n```\n##### Create a read-only file in your home directory?\n```bash\ntouch file; chmod 400 file\n```\n##### How do you see command line history in UNIX?\n```bash\nThe 'history' command can be used to get the list of commands that we are executed.\n```\n##### How to display the first 20 lines of a file?\n\nBy default, the head command displays the first 10 lines from a file. If we change the option of head, then we can display as many lines as we want.\n```bash\nhead -20 filename\n```\n-   An alternative solution is using the sed command\n```bash\nsed '21,$ d' filename\n```\n-   The d option here deletes the lines from 21 to the end of the file\n\n##### Write a command to print the last line of a file?\n\nThe tail command can be used to display the last lines from a file.\n```bash\ntail -1 filename\n```\n-   Alternative solutions are:\n```bash\nsed -n '$ p' filename\n\nawk 'END{print $0}' filename\n```\n##### How do you rename the files in a directory with _new as suffix?\n```bash\nls -lrt|grep '^-'| awk '{print \"mv \"$9\" \"$9\".new\"}' | sh\n```\n##### Write a command to convert a string from lower case to upper case?\n```bash\necho \"apple\" | tr [a-z] [A-Z]\n```\n##### Write a command to convert a string to Initcap.\n```bash\necho apple | awk '{print toupper(substr($1,1,1)) tolower(substr($1,2))}'\n```\n##### Write a command to redirect the output of date command to multiple files?\n\n-   The tee command writes the output to multiple files and also displays the output on the terminal.\n```bash\ndate | tee -a file1 file2 file3\n```\n##### How do you list the hidden files in current directory?\n```bash\nls -a | grep '^\\.'\n```\n##### List out some of the Hot Keys available in bash shell?\n```bash\nCtrl+l - Clears the Screen.\n\nCtrl+r - Does a search in previously given commands in shell.\n\nCtrl+u - Clears the typing before the hotkey.\n\nCtrl+a - Places cursor at the beginning of the command at shell.\n\nCtrl+e - Places cursor at the end of the command at shell.\n\nCtrl+d - Kills the shell.\n\nCtrl+z - Places the currently running process into background.\n```\n##### How do you make an existing file empty?\n```bash\ncat /dev/null \u003e  filename\n```\n##### How do you remove the first number on 10th line in file?\n```bash\nsed '10 s/[0-9][0-9]*//' \u003c filename\n```\n##### What is the difference between join -v and join -a?\n```bash\njoin -v : outputs only matched lines between two files.\n\njoin -a : In addition to the matched lines, this will output unmatched lines also.\n```\n##### How do you display from the 5th character to the end of the line from a file?\n```bash\ncut -c 5- filename\n```\n##### Display all the files in current directory sorted by size?\n```bash\nls -l | grep '^-' | awk '{print $5,$9}' |sort -n|awk '{print $2}'\n```\n##### Write a command to search for the file 'map' in the current directory?\n```bash\nfind -name map -type f\n```\n##### How to display the first 10 characters from each line of a file?\n```bash\ncut -c -10 filename\n```\n##### Write a command to remove the first number on all lines that start with \"@\"?\n```bash\nsed '\\,^@, s/[0-9][0-9]*//' \u003c filename\n```\n##### How to print the file names in a directory that has the word \"term\"?\n-   The '-l' option make the grep command to print only the filename without printing the content of the file. As soon \nas the grep command finds the pattern in a file, it prints the pattern and stops searching other lines in the file.\n```bash\ngrep -l term *\n```\n\n##### How to run awk command specified in a file?\n```bash\nawk -f filename\n```\n##### How do you display the calendar for the month march in the year 1985?\n\nThe cal command can be used to display the current month calendar. You can pass the month and year as arguments to display the required year, month combination calendar.\n```bash\ncal 03 1985\n```\nThis will display the calendar for the March month and year 1985.\n\n##### Write a command to find the total number of lines in a file?\n```bash\nwc -l filename\n```\n-   Other ways to pring the total number of lines are\n```bash\nawk 'BEGIN {sum=0} {sum=sum+1} END {print sum}' filename\n\nawk 'END{print NR}' filename\n```\n##### How to duplicate empty lines in a file?\n```bash\nsed '/^$/ p' \u003c filename\n```\n##### Explain iostat, vmstat and netstat?\n```bash\nIostat: reports on terminal, disk and tape I/O activity.\n\nVmstat: reports on virtual memory statistics for processes, disk, tape and CPU activity.\n\nNetstat: reports on the contents of network data structures.\n```\n##### How do you write the contents of 3 files into a single file?\n```bash\ncat file1 file2 file3 \u003e file\n```\n##### How to display the fields in a text file in reverse order?\n```bash\nawk 'BEGIN {ORS=\"\"} { for(i=NF;i\u003e0;i--) print $i,\" \"; print \"\\n\"}' filename\n```\n##### Write a command to find the sum of bytes (size of file) of all files in a directory.\n```bash\nls -l | grep '^-'| awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'\n```\n##### Write a command to print the lines which end with the word \"end\"?\n```bash\ngrep 'end$' filename\n```\n-   The '$' symbol specifies the grep command to search for the pattern at the end of the line.\n\n##### Write a command to select only those lines containing \"july\" as a whole word?\n```bash\ngrep -w july filename\n```\n-   The '-w' option makes the grep command to search for exact whole words. If the specified pattern is found in a \nstring, then it is not considered as a whole word. For example: In the string \"mikejulymak\", the pattern \"july\" is found. However \"july\" is not a whole word in that string.\n\n##### How to remove the first 10 lines from a file?\n```bash\nsed '1,10 d' \u003c filename\n```\n##### Write a command to duplicate each line in a file?\n```bash\nsed 'p' \u003c filename\n```\n##### How to extract the username from 'who am i' comamnd?\n```bash\nwhoami\n```\n##### Write a command to list the files in '/usr' directory that start with 'ch' and then display the number of lines in each file?\n```bash\nwc -l /usr/ch*\n```\n-   Another way is\n```bash\nfind /usr -name 'ch*' -type f -exec wc -l {} \\;\n```\n##### How to remove blank lines in a file ?\n```bash\ngrep -v ‘^$’ filename \u003e new_filename\n```\n##### How to display the processes that were run by your user name ?\n```bash\nps -aef | grep \u003cuser_name\u003e\n```\n##### Write a command to display all the files recursively with path under current directory?\n```bash\nfind . -depth -print\n```\n##### Display zero byte size files in the current directory?\n```bash\nfind -size 0 -type f\n```\n##### Write a command to display the third and fifth character from each line of a file?\n```bash\ncut -c 3,5 filename\n```\n##### Write a command to print the fields from 10th to the end of the line. The fields in the line are delimited by a comma?\n```bash\ncut -d',' -f10- filename\n```\n##### How to replace the word \"Gun\" with \"Pen\" in the first 100 lines of a file?\n```bash\nsed '1,00 s/Gun/Pen/' \u003c filename\n```\n##### Write a Unix command to display the lines in a file that do not contain the word \"RAM\"?\n```bash\ngrep -v RAM filename\n```\n-   The '-v' option tells the grep to print the lines that do not contain the specified pattern.\n\n##### How to print the squares of numbers from 1 to 10 using awk command\n```bash\nawk 'BEGIN { for(i=1;i\u003c=10;i++) {print \"square of\",i,\"is\",i*i;}}'\n```\n##### Write a command to display the files in the directory by file size?\n```bash\nls -l | grep '^-' |sort -nr -k 5\n```\n##### How to find out the usage of the CPU by the processes?\n\nThe ```bash top ``` bash utility can be used to display the CPU usage by the processes.\n\n##### Write a command to remove the prefix of the string ending with '/'.\n\n-   The basename utility deletes any prefix ending in /. The usage is mentioned below:\n```bash\nbasename /usr/local/bin/file\n```\n-   This will display only file\n\n##### How to display zero byte size files?\n```bash\nls -l | grep '^-' | awk '/^-/ {if ($5 !=0 ) print $9 }'\n```\n##### How to replace the second occurrence of the word \"bat\" with \"ball\" in a file?\n```bash\nsed 's/bat/ball/2' \u003c filename\n```\n##### How to remove all the occurrences of the word \"jhon\" except the first one in a line with in the entire file?\n```bash\nsed 's/jhon//2g' \u003c filename\n```\n##### How to replace the word \"lite\" with \"light\" from 100th line to last line in a file?\n```bash\nsed '100,$ s/lite/light/' \u003c filename\n```\n##### How to list the files that are accessed 5 days ago in the current directory?\n```bash\nfind -atime 5 -type f\n```\n##### How to list the files that were modified 5 days ago in the current directory?\n```bash\nfind -mtime 5 -type f\n```\n##### How to list the files whose status is changed 5 days ago in the current directory?\n```bash\nfind -ctime 5 -type f\n```\n##### How to replace the character '/' with ',' in a file?\n```bash\nsed 's/\\//,/' \u003c filename\n\nsed 's|/|,|' \u003c filename\n```\n##### Write a command to find the number of files in a directory.\n```bash\nls -l|grep '^-'|wc -l\n```\n##### Write a command to display your name 100 times.\n\n-   The Yes utility can be used to repeatedly output a line with the specified string or 'y'.\n```bash\nyes \u003cyour_name\u003e | head -100\n```\n##### Write a command to display the first 10 characters from each line of a file?\n```bash\ncut -c -10 filename\n```\n##### The fields in each line are delimited by comma. Write a command to display third field from each line of a file?\n```bash\ncut -d',' -f2 filename\n```\n##### Write a command to print the fields from 10 to 20 from each line of a file?\n```bash\ncut -d',' -f10-20 filename\n```\n##### Write a command to print the first 5 fields from each line?\n```bash\ncut -d',' -f-5 filename\n```\n##### By default the cut command displays the entire line if there is no delimiter in it. Which cut option is used to supress these kind of lines?\n\nThe ```bash-s``` option is used to supress the lines that do not contain the delimiter.\n\n##### Write a command to replace the word \"bad\" with \"good\" in file?\n```bash\nsed s/bad/good/ \u003c filename\n```\n##### Write a command to replace the word \"bad\" with \"good\" globally in a file?\n```bash\nsed s/bad/good/g \u003c filename\n```\n##### Write a command to replace the word \"apple\" with \"(apple)\" in a file?\n```bash\nsed s/apple/(\u0026)/ \u003c filename\n```\n##### Write a command to switch the two consecutive words \"apple\" and \"mango\" in a file?\n```bash\nsed 's/\\(apple\\) \\(mango\\)/\\2 \\1/' \u003c filename\n```\n##### Write a command to display the characters from 10 to 20 from each line of a file?\n```bash\ncut -c 10-20 filename\n```\n##### Write a command to print the lines that has the the pattern \"july\" in all the files in a particular directory?\n```bash\ngrep july *\n```\n-   This will print all the lines in all files that contain the word “july” along with the file name. If any of the \nfiles contain words like \"JULY\" or \"July\", the above command would not print those lines.\n\n##### Write a command to print the lines that has the word \"july\" in all the files in a directory and also suppress the filename in the output.\n```bash\ngrep -h july *\n```\n##### Write a command to print the lines that has the word \"july\" while ignoring the case.\n```bash\ngrep -i july *\n```\n-   The option i make the grep command to treat the pattern as case insensitive.\n\n##### When you use a single file as input to the grep command to search for a pattern, it won't print the filename in the output. Now write a grep command to print the filename in the output without using the '-H' option.\n```bash\ngrep pattern filename /dev/null\n```\n-   The /dev/null or null device is special file that discards the data written to it. So, the /dev/null is always an \nempty file.\n\n-   Another way to print the filename is using the '-H' option. The grep command for this is\n```bash\ngrep -H pattern filename\n```\n##### Write a command to print the file names in a directory that does not contain the word \"july\"?\n```bash\ngrep -L july *\n```\n-   The ``` bash'-L' ``` option makes the grep command to print the filenames that do not contain the specified \npattern.\n\n##### Write a command to print the line numbers along with the line that has the word \"july\"?\n```bash\ngrep -n july filename\n```\nThe ```bash'-n'``` option is used to print the line numbers in a file. The line numbers start from 1\n\n##### Write a command to print the lines that starts with the word \"start\"?\n```bash\ngrep '^start' filename\n```\nThe ```bash'^'``` symbol specifies the grep command to search for the pattern at the start of the line.\n\n##### In the text file, some lines are delimited by colon and some are delimited by space. Write a command to print the third field of each line.\n```bash\nawk '{ if( $0 ~ /:/ ) { FS=\":\"; } else { FS =\" \"; } print $3 }' filename\n```\n##### Write a command to print the line number before each line?\n```bash\nawk '{print NR, $0}' filename\n```\n##### Write a command to print the second and third line of a file without using NR.\n```bash\nawk 'BEGIN {RS=\"\";FS=\"\\n\"} {print $2,$3}' filename\n```\n##### How to create an alias for the complex command and remove the alias?\n\n-   The alias utility is used to create the alias for a command. The below command creates alias for ps -aef command.\n```bash\nalias pg='ps -aef'\n```\n-   If you use pg, it will work the same way as ps -aef.\n\n-   To remove the alias simply use the unalias command as\n```bash\nunalias pg\n```\n##### Write a command to display todays date in the format of 'yyyy-mm-dd'?\n\n-   The date command can be used to display todays date with time\n```bash\ndate '+%Y-%m-%d'\n```\n##### For LOOP\n\n1. Rename all \".old\" files in the current directory to \".bak\":\n```bash\nfor i in *.old   do  j=`echo $i|sed 's/old/bak/'`  mv $i $j  done\n```\n2. Change all instances of \"yes\" to \"no\" in all \".txt\" files in the current directory. Back up the original files to \".bak\".\n```bash\nfor i in *.txt do  j=`echo $i|sed 's/txt/bak/'`  mv $i $j   sed 's/yes/no/' $j \u003e $i  done\n```\n3. Loop thru a text file containing possible file names. If the file is readable, print the first line, otherwise print an error message:\n```bash\nfor i in `cat file_list.txt` do  if test -r $i   \n\n  then        \n\n   echo \"Here is the first line of file: $i\"       \n\n   sed 1q $i   \n\nelse\n\necho \"file $i cannot be open for reading.\"      \nfi  done\n```\n##### How to print/display the first line of a file?\n```bash\nhead -1 file.txt\n\nsed '2,$ d' file.txt\n```\n##### How to print/display the last line of a file?\n```bash\ntail -1 file.txt\n\nsed -n '$ p' test\n```\n##### How to display n-th line of a file?\n```bash\nsed –n '\u003cn\u003e p' file.txt\n\nsed –n '4 p' test\n\nhead -\u003cn\u003e file.txt | tail -1\n\nhead -4 file.txt | tail -1\n```\n##### How to remove the first line / header from a file?\n```bash\nsed '1 d' file.txt\n\nsed '1 d' file.txt \u003e new_file.txt\n\nmv new_file.txt file.txt\n\nsed –i '1 d' file.txt\n```\n##### How to remove the last line/ trailer from a file in Unix script?\n```bash\nsed –i '$ d' file.txt\n```\n##### How to remove certain lines from a file in Unix?\n```bash\nsed –i '5,7 d' file.txt\n```\n##### How to remove the last n-th line from a file?\n```bash\nsed –i '96,100 d' file.txt   # alternative to command [head -95 file.txt]\n\ntt=`wc -l file.txt | cut -f1 -d' '`;sed –i \"`expr $tt - 4`,$tt d\" test\n```\n##### How to check the length of any line in a file?\n```bash\nsed –n '\u003cn\u003e p' file.txt\n\nsed –n '35 p' file.txt | wc –c\n```\n##### How to check if a file is present in a particular directory in Unix?\n```bash\nls –l file.txt; echo $?\n```\n##### How to check all the running processes in Unix?\n```bash\nps –ef\n\nps aux\n\nps -e -o stime,user,pid,args,%mem,%cpu\n\nCombine multiple Rows to a Column – Oracle\n\nSELECT SUBSTR (SYS_CONNECT_BY_PATH (NAME , ','), 2) FRUITS_LIST\n\nFROM (SELECT NAME , ROW_NUMBER () OVER (ORDER BY NAME ) RN,\n\nCOUNT (*) OVER () CNT\n\nFROM FRUITS)\n\nWHERE RN = CNT\n\nSTART WITH RN = 1\n\nCONNECT BY RN = PRIOR RN + 1;\n```\n#####What is command to check space in Unix\n```bash\ndf -k\n```\n##### What is command to kill last background Job\n```bash\nkill $!\n```\n##### What is difference between diff and cmp command\n```bash\ncmp -It compares two files byte by byte and displays first mismatch.\n\ndiff -It displays all changes required to make files identical.\n```\n\n##### What does $# stands for\n```text\nIt will return the number of parameters passed as command line argument.Unix Commands\n```\n\n## 👬 Contribution\n- Open pull request with improvements\n- Discuss ideas in issues\n\n- Reach out with any feedback [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/anmol_nagpal.svg?style=social\u0026label=Follow%20%40anmol_nagpal)](https://twitter.com/anmol_nagpal)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanmolnagpal%2Fbash-commands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanmolnagpal%2Fbash-commands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanmolnagpal%2Fbash-commands/lists"}