InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Getting to the Bottom of Things in bash
Bash

Getting to the Bottom of Things in bash

InfinityCoder June 27, 2017

You work in a lot of narrow but deep directory structures, where all the content is at the bottom and you’re tired of having to manually cd so many levels.

1
alias bot='cd $(dirname $(find . | tail -1))'

This use of find in a large directory structure such as /usr could take a while and isn’t recommended.

Depending on how your directory structure is set up, this may not work for you; you’ll have to try it and see.

The find . will simply list all the files and directories in the current directory and below, the tail -1 will grab the last line, dirname will extract just the path, and cd will take you there.

It may be possible for you to tweak the command to get it to put you in the right place.

For example:

1
2
alias bot='cd $(dirname $(find . | sort -r | tail -5 | head -1))'
alias bot='cd $(dirname $(find . | sort -r | grep -v 'X11' | tail -3 | head -1))'

Keep trying the part in the inner-most parentheses, especially tweaking the find command, until you get the results you need.

Perhaps there is a key file or directory at the bottom of the structure, in which case the following function might work:

1
function bot { cd $(dirname $(find . | grep -e "$1" | head -1)); }

Note that aliases can’t use arguments, so this must be a function. We use grep rather than a -name argument to find because grep is much more flexible.

Depending on your structure, you might want to use tail instead of head. Again, test the find command first

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Searching for an SSN in bash
You need a regular expression to match a Social Security …

Searching for an SSN in bash

Parsing Command-Line Arguments in bash
You want to write a simple shell script to print …

Parsing Command-Line Arguments in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap