Folder shortcut in bash [closed] - bash

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I have a git repo in a folder that is inside multiple other folders. When I want to use the command line for git, I have to do cd /Desktop/.../.../.../.../repo_folder to do git commands. Is their any way that I can set a shortcut to a folder or get to a folder faster because having to type in a 70 character long path is not ideal
Thanks in advanced!

You can use the cdable_vars option of bash that allows you to call cd with a variable name. If the argument passed to cd is not a directory, then it is assumed to be a variable name and the value of the variable is used as the destination directory.
Example of use: if you put this in your ~/.bashrc:
alias show='cat ~/.dirs'
save () {
here=`pwd`
if (( $# == 0 )); then
name=`basename $here`
elif (( $# > 1 )); then
echo "usage: save [<name>]"
return -1
else
name=$1
fi
sed -i -e "/^$name=/d" ~/.dirs
echo "$name=\"$here\"" >> ~/.dirs
source ~/.dirs
}
source ~/.dirs
shopt -s cdable_vars
Then, when your current directory is one that you want to remember, just type:
save my_dir
and the next time you want to go there, just type:
cd my_dir
As long there is no my_dir directory where you type it, it will bring you where you want. The save argument is optional. If you do not provide it the defined short hand will be the base name of the current directory:
cd /Desktop/../../../../repo_folder
save
will define repo_folder as the short hand for this directory.
The ~/.dirs file contains your variable definitions for your favourite directories. You can edit it by hand, if you wish. These definitions are evaluated every time you launch a new bash shell. Beware they may overwrite others that you also need. If it is a problem, I advice you to chose unique short hands (my_dir_repo_folder instead of repo_folder). And remember the second pitfall, when you type:
cd foo
you can go either to the local sub-directory foo if there is one or to the directory for which you defined the foo short hand. And there is a third one: if you redefine a short hand, the previous one is overwritten. So, this trick is handy but somehow dangerous because when you cd you do not know any more if you are really where you want. Customizing your prompt to show the current path may be a good idea.
The show alias is just a way to list all currently defined short hands.

Just set "start in" param in your bash shortcut properties and every time you run bash it will open your repo folder :)

Related

How does autojump change the current working directory? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 14 days ago.
This post was edited and submitted for review 14 days ago.
Improve this question
autojump works by maintaining a database of the directories you use the most from the command line. Then you can cd to directories via shortcuts, e.g. to jump to a directory that contains "foo", I can just call j foo instead of cd /full/path/to/foo.
$ pwd
/some/directory
$ j foo
/full/path/to/foo
I'm trying to understand how autojump is able to change the directory by calling cd inside its bash script. As far as I know, such a script is executed in a separate shell. Here's the part in autojump's code that calls cd.
For example, calling this doesn't change the directory outside of the script, so how is autojump able to achieve it?
# myscript.sh
#!/bin/bash
cd path/to/foo
$ pwd
/some/directory
$ ./myscript.sh
/some/directory
How do zoxide and autojump change the current working directory?
Normally, with cd.
As far as I know, such a script is executed in a separate shell.
So they are not scripts, they are shell functions.
How is it able to call the function from the terminal in a way that it changed the directory in the terminal?
Like so:
$ cat mycdfunction.sh
mycdfunction() {
echo "jumping to my place"
cd tomyplace
}
$ . mycdfunction.sh
$ mycdfunction
jumping to my place
$ pwd
myplace

Is there any shorter syntax for changing several directory levels back up with bash cd? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
If I need to change to some directory several levels up, I usually do this in bash:
cd ../../../../some/other/folder
Since it is quite annoying to type all those periods and slashes, I was wondering if there is some shorter way to type it; like for example:
cd ..4/some/other/folder
I have not been able to find it so far from for example cd --help.
There's no standard way.
You can declare a function that takes a number of parent directories as the first argument, and the relative path as the second one:
cdu () {
local n=$1
local p=""
while ((n--)) ; do
p+=../
done
cd "$p/$2"
}
You can then shorten cd ../../../bin to cdu 3 bin
What I use is
alias ..='cd ..'
alias ...='cd ../..'
To get 6 levels up, I just type ... + Enter three times.
according to cd man page, the immediate answer is "no".
if it helps, you may add the following to your .bashrc:
export prev1=".."
export prev2="../.."
export prev3="../../.."
export prev4="../../../.."
and so on.
example:
export prev4="../../../.."
mkdir -p /1/2/3/4/5
cd /1/2/3/4/5
pwd => result is /1/2/3/4/5
cd $prev4
pwd => result is /1

What does touch * do? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I was asked a question in an interview recently - How you will delete a file named '*'?
So I tried creating a file as - touch *. When I list, I don't see this file. When I edit (vi *). I opens up a file with random text.
So what is touch *?
You should look up unix "globbing".
The shell will look at the command and replace * with every file (not starting with '.' or hidden) unless you escape this special character * just or put it in single quotes '*'
You can use this to apply a command to many files without actually listing them manually.
If you want to see what * expands and how you can escape it to you can run the following commands:
echo *
echo '*'
echo \*
You can also use this with more specific pattern like
<command> a* # run the command on the list of file starting with a
If you want to see what the terminal actually executed when you ran touch * run:
echo touch *
The touch command as others have explained before will either create a empty file if the filename does not exist, or update the last modified timestamp on existing files. So you just updated the last modified timestamps of every non hidden file in the directory you executed the command in.
Note that this happens before your command is ever called. And if the list of file is VERY large (millions) you will get an error as the list of files will be too large to fit into the command buffer (aka the string of your expanded command will be too large)
Further reading : https://www.linuxjournal.com/content/bash-extended-globbing
Shells expand *. touch * is touch every nonhidden file in the current directory. If you want to create a file literally named *, you need to quote it: touch \* (or touch '*' or touch "*" and the equivalent for other commands such as rm).
According to the man touch, touch changes a timestamp of a file. If the file doesn't exist, it creates it empty, unless -c or -h parameters are given.
In Bash, * is a globbing character which expands to every non-hidden file and directory in the current directory. That means touch * will change the timestamp of every file and directory in the current directory, if there is any, to the current date and time.

Extend .bash_profile for modularity? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have two laptops--one for work, one for personal stuff. My .bash_profile on each has a lot of the same functions and aliases. However, there is some stuff on my work laptop's .bash_profile that doesn't exist on my personal laptop's.
I'm wondering if there is a way I can make a "shared" .bash_profile or something, and extend functionality to it so it's easier to share my bash stuff between laptops.
I'm using a Mac if that matters; bash --version == 4.4.19
Sure it's possible, and not that uncommon to break up a .bash_profile into modules using the source command.
From your question it sounds like your .bash_profiles are almost identical, but the only difference is that you have work specific stuff on your work laptop. One way you could break it up would be to move your work aliases, functions, exports, etc. to their own file. We'll call it ~/.work_profile
Then in your ~/.bash_profile add the following.
[[ -f ~/.work_profile ]] && source ~/.work_profile
An explanation: [[ -f ~/.work_profile ]] tests that the regular file ~/.work_profile exists and will return true if it does.
&& source ~/.work_profile is a logical and. If the previous command returns true, then it will run this part and source your ~/.work_profile.
Solution 1:
Put common stuff in the shared .bash_profile
source ~/.bashrc.local at the end of .bash_profile
Each laptop has its own .bashrc.local.
Solution 2: Use one single .bash_profile on both laptops.
... common stuff ...
if [[ -f /a/file/only/on/work/laptop ]]; then
... now on work laptop ...
else
... now on home laptop ...
fi
I have 20+ Bash rc files so my real solution is a bit more complicated:
There's only one line in ~/.bashrc_profile:
source ~/.bashrc
All systems share the same ~/.bashrc in which the logic is like this:
# Each system has its own `.bashrc.pre' which defines the
# var `$RC_DIR' for top dir of all rc files (Bash, Vim, ...).
#
# I need to define my own `$RC_DIR' because my coworkers share
# some servers (using the same accounts) at work.
source ~/.bashrc.pre
# `rcfiles' is system specific which lists rc files to be
# loaded on that system. I maintain a `rcfiles.template'.
for file in $(< $RC_DIR/bash/rcfiles); do
source $file
done
# `.bashrc.post' is system specific
source ~/.bashrc.post
You can either use the profile file in /etc if you have root, or use source filename to include other (common) file.
You may also want to have a look at this question explaining the different loadable files.

Building Program-Specific Shortcuts in UNIX [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a program, called carmel, which I can run from the command line via:
carmel -h
or whichever suffix I chose. When loading a file, I can say:
carmel fsa1.fst where fsa1.fst is located in my heme folder, /Users/adam/.
I would prefer to have the default file location be, e.g., /Users/adam/carmel/files, and would prefer to not type that in every time. Is there a way to let UNIX know, when I type carmel to then look in that location?
There is no standard Unix shortcut for this behaviour. Some applications will check an environment variable to see where their files are. but looking at carmel/src/carmel.cc on GitHub, I'd say you'd have to write a wrapper script. Like this:
#!/usr/bin/env bash
# Save as ${HOME}/bin/carmel and ensure ${HOME}/bin is before
# ${carmel_bin_dir} in your ${PATH}. Also ensure this script
# has the executable bit set.
carmel_bin_dir=/usr/local/bin # TODO change this?
working_directory=${CARMEL_HOME-${HOME}/carmel/files}
if [[ ! -d "${working_directory}" ]]; then
echo "${working_directory} does not exist. Creating."
mkdir -p "${working_directory}" || echo "Failed to create ${working_directory}"
fi
pushd "${working_directory}"
echo "Launching ${carmel_bin_dir}/carmel ${#} from $(pwd)..."
${carmel_bin_dir}/carmel ${#}
popd
Alternatively, since the source is freely available, you could add some code to read ${CARMEL_HOME} (or similar) and submit this as a pull request.
Good luck!

Resources