Web-Dev-Hub-Docs
Web Development Frameworks & Libraries
Productivity
Misc
Backend
Networking
Science & Tech (Innovation)
Reading
Docs
Code Editors & Tools
Free-Stuff
Blockchain & Crypto
Data Structures & Interviewing
WEBDEV-Bootcamp-Notes
Group 1
Powered By GitBook
Bash Commands That Save Me Time and Frustration
Here’s a list of bash commands that stand between me and insanity.
​

Bash Commands That Save Me Time and Frustration

Here’s a list of bash commands that stand between me and insanity.

This article will be accompanied by the following github repository which will contain all the commands listed as well as folders that demonstrate before and after usage!
The readme for this git repo will provide a much more condensed list… whereas this article will break up the commands with explanations… images & links!
I will include the code examples as both github gists (for proper syntax highlighting) and as code snippets adjacent to said gists so that they can easily be copied and pasted… or … if you’re like me for instance; and like to use an extension to grab the markdown content of a page… the code will be included rather than just a link to the gist!
​
​

Here’s a Cheatsheet:

Getting Started (Advanced Users Skip Section):

​

✔ Check the Current Directory ➡ pwd:

On the command line, it’s important to know the directory we are currently working on. For that, we can use pwd command.
​
It shows that I’m working on my Desktop directory.

✔ Display List of Files ➡ ls:

To see the list of files and directories in the current directory use ls command in your CLI.
​
Shows all of my files and directories of my Desktop directory.
  • To show the contents of a directory pass the directory name to the ls command i.e. ls directory_name.
  • Some useful ls command options:-
OptionDescriptionls -alist all files including hidden file starting with ‘.’ls -llist with the long formatls -lalist long format including hidden files

✔ Create a Directory ➡ mkdir:

We can create a new folder using the mkdir command. To use it type mkdir folder_name.
​
Use `ls` command to see the directory is created or not.
I created a cli-practice directory in my working directory i.e. Desktop directory.

✔ Move Between Directories ➡ cd:

It’s used to change directory or to move other directories. To use it type cd directory_name.
​
Can use `pwd` command to confirm your directory name.
Changed my directory to the cli-practice directory. And the rest of the tutorial I’m gonna work within this directory.

✔ Parent Directory ➡ ..:

We have seen cd command to change directory but if we want to move back or want to move to the parent directory we can use a special symbol .. after cd command, like cd ..

✔ Create Files ➡ touch:

We can create an empty file by typing touch file_name. It's going to create a new file in the current directory (the directory you are currently in) with your provided name.
​
I created a hello.txt file in my current working directory. Again you can use `ls` command to see the file is created or not.
Now open your hello.txt file in your text editor and write Hello Everyone! into your hello.txt file and save it.

✔ Display the Content of a File ➡ cat:

We can display the content of a file using the cat command. To use it type cat file_name.
​
Shows the content of my hello.txt file.

✔ Move Files & Directories ➡ mv:

To move a file and directory, we use mv command.
By typing mv file_to_move destination_directory, you can move a file to the specified directory.
By entering mv directory_to_move destination_directory, you can move all the files and directories under that directory.
Before using this command, we are going to create two more directories and another txt file in our cli-practice directory.
mkdir html css touch bye.txt
​
Yes, we can use multiple directories & files names one after another to create multiple directories & files in one command.
Moved my bye.txt file into my css directory and then moved my css directory into my html directory.

✔ Rename Files & Directories ➡ mv:

mv command can also be used to rename a file and a directory.
You can rename a file by typing mv old_file_name new_file_name & also rename a directory by typing mv old_directory_name new_directory_name.
​
Renamed my hello.txt file to the hi.txt file and html directory to the folder directory.

✔ Copy Files & Directories ➡ cp:

To do this, we use the cp command.
  • You can copy a file by entering cp file_to_copy new_file_name.
​
Copied my hi.txt file content into hello.txt file. For confirmation open your hello.txt file in your text editor.
  • You can also copy a directory by adding the -r option, like cp -r directory_to_copy new_directory_name.
The -r option for "recursive" means that it will copy all of the files including the files inside of subfolders.
​
Here I copied all of the files from the folder to folder-copy.

✔ Remove Files & Directories ➡ rm:

To do this, we use the rm command.
  • To remove a file, you can use the command like rm file_to_remove.
​
Here I removed my hi.txt file.
  • To remove a directory, use the command like rm -r directory_to_remove.
​
I removed my folder-copy directory from my cli-practice directory i.e. current working directory.

✔ Clear Screen ➡ clear:

Clear command is used to clear the terminal screen.

✔ Home Directory ➡ ~:

The Home directory is represented by ~. The Home directory refers to the base directory for the user. If we want to move to the Home directory we can use cd ~ command. Or we can only use cd command.
​

MY COMMANDS:

1.) Recursively unzip zip files and then delete the archives when finished:

here is a folder containing the before and after… I had to change folder names slightly due to a limit on the length of file-paths in a github repo.
1
find . -name "*.zip" | while read filename; do unzip -o -d "`dirname "$filename"`" "$filename"; done;
2
​
3
find . -name "*.zip" -type f -print -delete
Copied!
​

2.) Install node modules recursively:

1
npm i -g recursive-install
2
​
3
npm-recursive-install
Copied!
​

3.) Clean up unnecessary files/folders in git repo:

1
find . -empty -type f -print -delete #Remove empty files
2
​
3
# -------------------------------------------------------
4
find . -empty -type d -print -delete #Remove empty folders
5
​
6
# -------------------------------------------------------
7
​
8
# This will remove .git folders... .gitmodule files as well as .gitattributes and .gitignore files.
9
​
10
find . \( -name ".git" -o -name ".gitignore" -o -name ".gitmodules" -o -name ".gitattributes" \) -exec rm -rf -- {} +
11
​
12
# -------------------------------------------------------
13
​
14
# This will remove the filenames you see listed below that just take up space if a repo has been downloaded for use exclusively in your personal file system (in which case the following files just take up space)# Disclaimer... you should not use this command in a repo that you intend to use with your work as it removes files that attribute the work to their original creators!
15
​
16
find . \( -name "*SECURITY.txt" -o -name "*RELEASE.txt" -o -name "*CHANGELOG.txt" -o -name "*LICENSE.txt" -o -name "*CONTRIBUTING.txt" -name "*HISTORY.md" -o -name "*LICENSE" -o -name "*SECURITY.md" -o -name "*RELEASE.md" -o -name "*CHANGELOG.md" -o -name "*LICENSE.md" -o -name "*CODE_OF_CONDUCT.md" -o -name "\*CONTRIBUTING.md" \) -exec rm -rf -- {} +
Copied!

In Action:

The following output from my bash shell corresponds to the directory:

which was created by running the aforementioned commands in in a perfect copy of this directory:

…..below is the terminal output for the following commands:
1
pwd
2
/mnt/c/Users/bryan/Downloads/bash-commands/steps/3-clean-up-fluf/DS-ALGO-OFFICIAL-master
Copied!
After printing the working directory for good measure:
1
find . -empty -type f -print -delete
Copied!
The above command deletes empty files recursively starting from the directory in which it was run:
1
./CONTENT/DS-n-Algos/File-System/file-utilities/node_modules/line-reader/test/data/empty_file.txt
2
./CONTENT/DS-n-Algos/_Extra-Practice/free-code-camp/nodejs/http-collect.js
3
./CONTENT/Resources/Comments/node_modules/mime/.npmignore
4
./markdown/tree2.md
5
./node_modules/loadashes6/lodash/README.md
6
./node_modules/loadashes6/lodash/release.md
7
./node_modules/web-dev-utils/Markdown-Templates/Markdown-Templates-master/filled-out-readme.md
8
|01:33:16|[email protected]:[DS-ALGO-OFFICIAL-master] DS-ALGO-OFFICIAL-master_exitstatus:0[â•—___________o>
Copied!
The command seen below deletes empty folders recursively starting from the directory in which it was run:
1
find . -empty -type d -print -delete
Copied!
The resulting directories….
1
|01:33:16|[email protected]:[DS-ALGO-OFFICIAL-master] DS-ALGO-OFFICIAL-master_exitstatus:0[â•—___________o>
2
​
3
find . -empty -type d -print -delete
4
./.git/branches
5
./.git/objects/info
6
./.git/refs/tags
7
|01:33:31|[email protected]:[DS-ALGO-OFFICIAL-master] DS-ALGO-OFFICIAL-master_exitstatus:0[â•—___________o>
Copied!
The command seen below deletes .git folders as well as .gitignore, .gitattributes, .gitmodule files
1
find . \( -name ".git" -o -name ".gitignore" -o -name ".gitmodules" -o -name ".gitattributes" \) -exec rm -rf -- {} +
Copied!
The command seen below deletes most SECURITY, RELEASE, CHANGELOG, LICENSE, CONTRIBUTING, & HISTORY files that take up pointless space in repo’s you wish to keep exclusively for your own reference.

!!!Use with caution as this command removes the attribution of the work from it’s original authors!!!!!

​
!!!Use with caution as this command removes the attribution of the work from it’s original authors!!!!!find . \( -name "*SECURITY.txt" -o -name "*RELEASE.txt" -o -name "*CHANGELOG.txt" -o -name "*LICENSE.txt" -o -name "*CONTRIBUTING.txt" -name "*HISTORY.md" -o -name "*LICENSE" -o -name "*SECURITY.md" -o -name "*RELEASE.md" -o -name "*CHANGELOG.md" -o -name "*LICENSE.md" -o -name "*CODE_OF_CONDUCT.md" -o -name "*CONTRIBUTING.md" \) -exec rm -rf -- {} +
​
1
#!/bin/sh
2
# find ./ | grep -i "\.*quot; >files
3
find ./ | sed -E -e 's/([^ ]+[ ]+){8}//' | grep -i "\.*quot;>files
4
listing="files"
5
out=""
6
html="index.html"
7
out="basename $out.html"
8
html="index.html"
9
cmd() {
10
echo ' <!DOCTYPE html>'
11
echo '<html>'
12
echo '<head>'
13
echo ' <meta http-equiv="Content-Type" content="text/html">'
14
echo ' <meta name="Author" content="Bryan Guner">'
15
echo '<link rel="stylesheet" href="./assets/prism.css">'
16
echo ' <link rel="stylesheet" href="./assets/style.css">'
17
echo ' <script async defer src="./assets/prism.js"></script>'
18
echo " <title> directory </title>"
19
echo ""
20
echo '<style>'
21
echo ' a {'
22
echo ' color: black;'
23
echo ' }'
24
echo ''
25
echo ' li {'
26
echo ' border: 1px solid black !important;'
27
echo ' font-size: 20px;'
28
echo ' letter-spacing: 0px;'
29
echo ' font-weight: 700;'
30
echo ' line-height: 16px;'
31
echo ' text-decoration: none !important;'
32
echo ' text-transform: uppercase;'
33
echo ' background: #194ccdaf !important;'
34
echo ' color: black !important;'
35
echo ' border: none;'
36
echo ' cursor: pointer;'
37
echo ' justify-content: center;'
38
echo ' padding: 30px 60px;'
39
echo ' height: 48px;'
40
echo ' text-align: center;'
41
echo ' white-space: normal;'
42
echo ' border-radius: 10px;'
43
echo ' min-width: 45em;'
44
echo ' padding: 1.2em 1em 0;'
45
echo ' box-shadow: 0 0 5px;'
46
echo ' margin: 1em;'
47
echo ' display: grid;'
48
echo ' -webkit-border-radius: 10px;'
49
echo ' -moz-border-radius: 10px;'
50
echo ' -ms-border-radius: 10px;'
51
echo ' -o-border-radius: 10px;'
52
echo ' }'
53
echo ' </style>'
54
echo '</head>'
55
echo '<body>'
56
echo ""
57
# continue with the HTML stuff
58
echo ""
59
echo ""
60
echo "<ul>"
61
awk '{print "<li><a href=\""$1"\">",$1,"&nbsp;</a></li>"}' $listing
62
# awk '{print "<li>"};
63
# {print " <a href=\""$1"\">",$1,"</a></li>&nbsp;"}' \ $listing
64
echo ""
65
echo "</ul>"
66
echo "</body>"
67
echo "</html>"
68
}
69
cmd $listing --sort=extension >>$html
Copied!

In Action:

I will use this copy of my Data Structures Practice Site to demonstrate the result:
​
#### The result is a index.html file that contains a list of links to each file in the directory:
here is a link to and photo of the resulting html file:
​
​
​
1
wget -r -A.pdf https://overapi.com/gitwget --wait=2 --level=inf --limit-rate=20K --recursive --page-requisites --user-agent=Mozilla --no-parent --convert-links --adjust-extension --no-clobber -e robots=off
Copied!
The result is stored in this directory:​
​
​
​

6.)Recursively remove lines of text containing the string badFolder from files in the working directory.

1
find . -type f -exec sed -i '/badFolder/d' ./* {} \;
2
​
3
# OR
4
​
5
find . -name 'result.md' -type f -exec sed -i '/badFolder/d' ./* {} \;
Copied!
As an example I will run this command on a file containing the text:
1
Hacks Blog
2
​
3
Read more at hacks.mozilla.org
4
​
5
badFolder
6
​
7
badFolder Implementing Private Fields for JavaScript
8
​
9
When implementing a language feature for JavaScript, an implementer must make decisions about how the language in the specification maps to the implementation. Private fields is an example of where the specification language and implementation reality diverge, at least in SpiderMonkey– the JavaScript engine which powers Firefox. To understand more, I’ll explain what private fields are, a couple of models for thinking about them, and explain why our implementation diverges from the specification language.The post Implementing Private Fields for JavaScript appeared first on Mozilla Hacks - the Web developer blog.
10
​
11
Posted Tuesday, June 8, 2021 by Matthew Gaudet
12
​
13
Looking fine with Firefox 89
14
​
15
Firefox 89 has smartened up and brings with it a slimmed-down, slightly more minimalist interface.badFolder Along with this new look, we get some great styling features including a force-colours feature for media queries and better control over how fonts are displayed. The long-awaited top-level await keyword for JavaScript modules is now enabled, as well as the PerformanceEventTiming interface, which is another addition to the performance suite of APIs: 89 really has been working out!The post Looking fine with Firefox 89 appeared first on Mozilla Hacks - the Web developer blog.
16
​
17
badFolder
18
​
19
Posted Tuesday, June 1, 2021 by Chris Mills
20
​
21
badFolder
22
​
23
Improving Firefox stability on Linux
24
​
25
Roughly a year ago at Mozilla we started an effort to improve Firefox stability on Linux. This effort quickly became an example of good synergies between FOSS projects.The post Improving Firefox stability on Linux appeared first on Mozilla Hacks - the Web developer blog.
26
​
27
Posted Wednesday, May 19, 2021 by Gabriele Svelto badFolder
28
​
29
Introducing Firefox’s new Site Isolation Security Architecture
30
​
31
Like any web browser, Firefox loads code from untrusted and potentially hostile websites and runs it on your computer. To protect you against new types of attacks from malicious sites and to meet the security principles of Mozilla, we set out to redesign Firefox on desktop.The post Introducing Firefox’s new Site Isolation Security Architecture appeared first on Mozilla Hacks - the Web developer blog.
32
​
33
Posted Tuesday, May 18, 2021 by Anny Gakhokidze
34
​
35
Pyodide Spin Out and 0.17 Release
36
​
37
We are happy to announce that Pyodide has become an independent and community-driven project. We are also pleased to announce the 0.17 release for Pyodide with many new features and improvements. Pyodide consists of the CPython 3.8 interpreter compiled to WebAssembly which allows Python to run in the browser.The post Pyodide Spin Out and 0.17 Release appeared first on Mozilla Hacks - the Web developer blog. badFolder
38
​
39
Posted Thursday, April 22, 2021 by Teon Brooks
Copied!
I modified the command slightly to apply only to files called ‘result.md’:
The result is :
1
Hacks Blog
2
​
3
Read more at hacks.mozilla.org
4
​
5
When implementing a language feature for JavaScript, an implementer must make decisions about how the language in the specification maps to the implementation. Private fields is an example of where the specification language and implementation reality diverge, at least in SpiderMonkey– the JavaScript engine which powers Firefox. To understand more, I’ll explain what private fields are, a couple of models for thinking about them, and explain why our implementation diverges from the specification language.The post Implementing Private Fields for JavaScript appeared first on Mozilla Hacks - the Web developer blog.
6
​
7
Posted Tuesday, June 8, 2021 by Matthew Gaudet
8
​
9
Looking fine with Firefox 89
10
​
11
Posted Tuesday, June 1, 2021 by Chris Mills
12
​
13
Improving Firefox stability on Linux
14
​
15
Roughly a year ago at Mozilla we started an effort to improve Firefox stability on Linux. This effort quickly became an example of good synergies between FOSS projects.The post Improving Firefox stability on Linux appeared first on Mozilla Hacks - the Web developer blog.
16
​
17
Introducing Firefox’s new Site Isolation Security Architecture
18
​
19
Like any web browser, Firefox loads code from untrusted and potentially hostile websites and runs it on your computer. To protect you against new types of attacks from malicious sites and to meet the security principles of Mozilla, we set out to redesign Firefox on desktop.The post Introducing Firefox’s new Site Isolation Security Architecture appeared first on Mozilla Hacks - the Web developer blog.
20
​
21
Posted Tuesday, May 18, 2021 by Anny Gakhokidze
22
​
23
Pyodide Spin Out and 0.17 Release
24
​
25
Posted Thursday, April 22, 2021 by Teon Brooks
Copied!
​
​
the test.txt and result.md files can be found here:
​

7.) Execute command recursively:

Here I have modified the command I wish to run recursively to account for the fact that the ‘find’ command already works recursively, by appending the -maxdepth 1 flag…
I am essentially removing the recursive action of the find command…
That way, if the command affects the more deeply nested folders we know the outer RecurseDirs function we are using to run the find/pandoc line once in every subfolder of the working directory… is working properly!
​
**Run in the folder shown to the left… we would expect every .md file to be accompanied by a newly generated html file by the same name.**
The results of said operation can be found in the following directory​

In Action:

🢃 Below 🢃
​
​

The final result is:

​
​
If you want to run any bash script recursively all you have to do is substitue out line #9 with the command you want to run once in every sub-folder.
1
function RecurseDirs ()
2
{
3
oldIFS=$IFS
4
IFS=#x27;\n'
5
for f in "[email protected]"
6
do
7
​
8
#Replace the line below with your own command!
9
​
10
#find ./ -iname "*.md" -maxdepth 1 -type f -exec sh -c 'pandoc --standalone "${0}" -o "${0%.md}.html"' {} \;
11
​
12
#####################################################
13
# YOUR CODE BELOW!
14
​
15
#####################################################
16
​
17
if [[ -d "${f}" ]]; then
18
cd "${f}"
19
RecurseDirs $(ls -1 ".")
20
cd ..
21
fi
22
done
23
IFS=$oldIFS
24
}
25
RecurseDirs "./"
Copied!
​

TBC….

Here are some of the other commands I will cover in greater detail… at a later time:

9. Copy any text between <script> tags in a file called example.html to be inserted into a new file: out.js

1
sed -n -e '/<script>/,/<\/script>/p' example.html >out.js
Copied!
​

10. Recursively Delete node_modules folders

1
find . -name 'node_modules' -type d -print -prune -exec rm -rf '{}' +
Copied!
​

11. Sanatize file and folder names to remove illegal characters and reserved words.

1
sanitize() {
2
shopt -s extglob;
3
​
4
filename=$(basename "$1")
5
directory=$(dirname "$1")
6
​
7
filename_clean=$(echo "$filename" | sed -e 's/[\\/:\*\?"<>\|\x01-\x1F\x7F]//g' -e 's/^\(nul\|prn\|con\|lpt[0-9]\|com[0-9]\|aux\)\(\.\|$\)//i' -e 's/^\.*$//' -e 's/^$/NONAME/')
8
​
9
if (test "$filename" != "$filename_clean")
10
then
11
mv -v "$1" "$directory/$filename_clean"
12
fi
13
}
14
​
15
export -f sanitize
16
​
17
sanitize_dir() {
18
find "$1" -depth -exec bash -c 'sanitize "$0"' {} \;
19
​
20
}
21
​
22
sanitize_dir '/path/to/somewhere'
Copied!
​

12. Start postgresql in terminal

1
sudo -u postgres psql
Copied!
​

13. Add closing body and script tags to each html file in working directory.

1
for f in * ; do
2
mv "$f" "$f.html"
3
doneecho "<form>
4
<input type="button" value="Go back!" onclick="history.back()">
5
</form>
6
</body></html>" | tee -a *.html
Copied!
​

14. Batch Download Videos

1
#!/bin/bash
2
​
3
link="#insert url here#"
4
#links were a set of strings with just the index of the video as the variable
5
​
6
num=3
7
#first video was numbered 3 - weird.
8
​
9
ext=".mp4"
10
​
11
while [ $num -le 66 ]
12
do
13
wget $link$num$ext -P ~/Downloads/
14
num=$(($num+1))
15
done
Copied!
​

15. Change File Extension from ‘.txt’ to .doc for all files in working directory.

1
sudo apt install rename
2
​
3
rename 's/\.txt$/.doc/' *.txt
Copied!

16. Recursivley change any file with extension .js.download to .js

1
find . -name "*.\.js\.download" -exec rename 's/\.js\.download$/.js/' '{}' +
Copied!
​

17. Copy folder structure including only files of a specific extension into an ouput Folder

1
find . -name '*.md' | cpio -pdm './../outputFolder'
Copied!
​

Discover More:

Part 2 of this series:

​
​Canonical link​
Exported from Medium on August 31, 2021.
Last modified 1d ago