Bash Commands That Save Me Time and Frustration

Bash Commands

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.

https://bryanguner.medium.com/a-list-of-all-of-my-articles-to-link-to-future-posts-1f6f88ebdf5b

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!

bgoonz/bash-commands-walkthrough _to accompany the medium article I am writing. Contribute to bgoonz/bash-commands-walkthrough development by creating an…_github.com

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.


2.) Install node modules recursively:


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

In Action:

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

bgoonz/bash-commands-walkthrough _Deployment github-pages Navigation Big O notation is the language we use for talking about how long an algorithm takes…_github.com

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

bgoonz/DS-ALGO-OFFICIAL _Deployment github-pages Navigation Big O notation is the language we use for talking about how long an algorithm takes…_github.com

…..below is the terminal output for the following commands:

After printing the working directory for good measure:

The above command deletes empty files recursively starting from the directory in which it was run:

The command seen below deletes empty folders recursively starting from the directory in which it was run:

The resulting directories….

The command seen below deletes .git folders as well as .gitignore, .gitattributes, .gitmodule files

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!!!!!!!!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 -- {} +


In Action:

I will use this copy of my Data Structures Practice Site to demonstrate the result:

side-projects-42/DS-Bash-Examples-Deploy _Deployment github-pages Navigation Big O notation is the language we use for talking about how long an algorithm takes…_github.com

#### 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:

index.html _CONTENT/DS-n-Algos/_quirky-meninsky-4181b5.netlify.app


The result is stored in this directory:


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

As an example I will run this command on a file containing the text:

I modified the command slightly to apply only to files called 'result.md':

The result is :

the test.txt and result.md files can be found here:

bgoonz/bash-commands-walkthrough _to accompany the medium article I am writing. Contribute to bgoonz/bash-commands-walkthrough development by creating an…_github.com


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.


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


10. Recursively Delete node_modules folders


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


12. Start postgresql in terminal


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


14. Batch Download Videos


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

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


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


Discover More:

Web-Dev-Hub _Memoization, Tabulation, and Sorting Algorithms by Example Why is looking at runtime not a reliable method of…_bgoonz-blog.netlify.app

Part 2 of this series:

Medium Continued!!!medium.com


By Bryan Guner on June 29, 2021.

Canonical link

Exported from Medium on August 31, 2021.

Resources:

1. Remove spaces from file and folder names and then remove numbers from files and folder names....

Description: need to : sudo apt install rename

Notes: Issue when renaming file without numbers collides with existing file name...

code:


2. Download Website Using Wget:

Description:

Notes: ==> sudo apt install wget

code:


3. Clean Out Messy Git Repo:

Notes: To clear up clutter in repositories that only get used on your local machine.

code:


4. clone all of a user's git repositories

Description: clone all of a user or organization's git repositories.

Notes:

code:

Generalized:

Clone all Git User

Clone all Git Organization:


5. Git Workflow

Description:

code:


6. Recursive Unzip In Place

Description: recursively unzips folders and then deletes the zip file by the same name.

Notes:

code:


7. git pull keeping local changes:

Description:

Notes:

code:


8. Prettier Code Formatter:

Description:

Notes:

code:


9. Pandoc

Description:

Notes:

code:


10. Gitpod Installs

Description:

Notes:

code:


11. Repo Utils Package:

Description: my standard repo utis package

Notes:

code:


12. Unix Tree Package Usage:

Description:

Notes:

code:


13. Find & Replace string in file & folder names recursively..

Description:

Notes:

code:


14. Remove double extensions :

Description:

Notes:

code:


15. Truncate folder names down to 12 characters:

Description:

Notes:

code:


16.Appendir.js

Description: combine the contents of every file in the contaning directory.

Notes: this includes the contents of the file it's self...

code:


17. Replace space in filename with underscore

Description: followed by replace '#' with '_' in directory name

Notes: Can be re-purposed to find and replace any set of strings in file or folder names.

code:


18. Filter & delete files by name and extension

Description:

Notes:

code:


19. Remove lines containing string:

Description:

Notes: Remove lines not containing '.js'

code:


20. Remove duplicate lines from a text file

Description:

Notes: //...syntax of uniq...// $uniq [OPTION] [INPUT[OUTPUT]] The syntax of this is quite easy to understand. Here, INPUT refers to the input file in which repeated lines need to be filtered out and if INPUT isn't specified then uniq reads from the standard input. OUTPUT refers to the output file in which you can store the filtered output generated by uniq command and as in case of INPUT if OUTPUT isn't specified then uniq writes to the standard output.

Now, let's understand the use of this with the help of an example. Suppose you have a text file named kt.txt which contains repeated lines that needs to be omitted. This can simply be done with uniq.

code:


21. Remove lines containing string:

Description:

Notes:

code:


22. Zip directory excluding .git and node_modules all the way down (Linux)

Description:

Notes:

code:


23. Delete files containing a certain string:

Description:

Notes:

code:


24.

Description:

Notes:

code:


25. Index of Iframes

Description: Creates an index.html file that contains all the files in the working directory or any of it's sub folders as iframes instead of anchor tags.

Notes: Useful Follow up Code:

code:


26. Filter Corrupted Git Repo For Troublesome File:

Description:

Notes:

code:


27. OVERWRITE LOCAL CHANGES:

Description:

Important: If you have any local changes, they will be lost. With or without --hard option, any local commits that haven't been pushed will be lost.[*] If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.

Notes: First, run a fetch to update all origin/ refs to latest:

code:


28. Remove Submodules:

Description: To remove a submodule you need to:

Notes:

Delete the relevant section from the .gitmodules file. Stage the .gitmodules changes git add .gitmodules Delete the relevant section from .git/config. Run git rm --cached path_to_submodule (no trailing slash). Run rm -rf .git/modules/path_to_submodule (no trailing slash). Commit git commit -m "Removed submodule " Delete the now untracked submodule files rm -rf path_to_submodule

code:


29. GET GISTS

Description:

Notes:

code:


30. Remove Remote OriginL

Description:

Notes:

code:


31. just clone .git folder:

Description:

Notes:

code:


32. Undo recent pull request:

Description:

Notes:

code:


33. Lebab

Description: ES5 --> ES6

Notes:

code:


34. Troubleshoot Ubuntu Input/Output Error

Description: Open Powershell as Administrator...

Notes:

code:


35. Export Medium as Markdown

Description:

Notes:

code:


36. Delete files in violation of a given size range (100MB for git)

Description:

Notes:

code:


Description:

Notes:

code:


38. Kill all node processes

Description:

Notes:

code:


39. Remove string from file names recursively

Description: In the example below I am using this command to remove the string "-master" from all file names in the working directory and all of it's sub directories.

code:

Notes: The same could be done for folder names by changing the -type f flag (for file) to a -type d flag (for directory)


40. Remove spaces from file and folder names recursively

Description: replaces spaces in file and folder names with an _ underscore

Notes: need to run sudo apt install rename to use this command

code:


41. Zip Each subdirectories in a given directory into their own zip file

Description:

Notes:

code:


42.

Description:

Notes:

code:


43.

Description:

Notes:

code:


44.

Description:

Notes:

code:


45.

Description:

Notes:

code:


46.

Description:

Notes:

code:


47.

Description:

Notes:

code:


48.

Description:

Notes:

code:


49.

Description:

Notes:

code:


50.

Description:

Notes:

code:


51.

Description:

Notes:

code:


52.

Description:

Notes:

code:


53.

Description:

Notes:

code:


54.

Description:

Notes:

code:


55.

Description:

Notes:

code:


56.

Description:

Notes:

code:


57.

Description:

Notes:

code:


58.

Description:

Notes:

code:


59.

Description:

Notes:

code:


60.

Description:

Notes:

code:


61.

Description:

Notes:

code:


62.

Description:

Notes:

code:


63.

Description:

Notes:

code:


64.

Description:

Notes:

code:


65.

Description:

Notes:

code:


66.

Description:

Notes:

code:


67.

Description:

Notes:

code:


68.

Description:

Notes:

code:


69.

Description:

Notes:

code:


70.

Description:

Notes:

code:


71.

Description:

Notes:

code:


72.

Description:

Notes:

code:


73.

Description:

Notes:

code:


74.

Description:

Notes:

code:


75.

Description:

Notes:

code:


76.

Description:

Notes:

code:


77.

Description:

Notes:

code:


78.

Description:

Notes:

code:


79.

Description:

Notes:

code:


80.

Description:

Notes:

code:


81.

Description:

Notes:

code:


82.

Description:

Notes:

code:


83.

Description:

Notes:

code:


84.

Description:

Notes:

code:


85.

Description:

Notes:

code:


86.

Description:

Notes:

code:


87.

Description:

Notes:

code:


88.

Description:

Notes:

code:


89.

Description:

Notes:

code:


90.

Description:

Notes:

code:


91. Unzip PowerShell

Description:

Notes:

code:


92. return to bash from zsh

Description:

Notes:

code:


Description: to working directory

Notes:

code:


94. auto generate readme

Description: rename existing readme to blueprint.md

Notes:

code:


95. Log into postgres:

Description:

Notes:

code:


96. URL To Subscribe To YouTube Channel

Description:

Notes:

code:


97. Embed Repl.it In Medium Post:

code:


98.

Description:

Notes:

code:


99. Cheat Sheet

Description:

Notes:

code:


Last updated

Was this helpful?