How can I save the path to a frequently used directory in UNIX? - unix

Is there a way to save the path to a frequently used directory in UNIX, so instead of having to manually cd /path/to/directory I can just enter a shortcut cd myFavoritePath ??

Define your favorite directories in CDPATH environment variable. It's a colon-separated list of search paths available to the cd command. You should specify not a directory you want to switch but parent directory.
Here is brief info about it: http://docstore.mik.ua/orelly/unix/upt/ch14_05.htm
For example you have three directories you work with frequently:
/home/user/scripts/favorite/
/var/log/
/var/lib/
add to your ~/.bash_profile (or another shell profile file you use) the next line:
export CDPATH=.:/home/user/scripts:/var
In the example below I just redefine CDPATH in shell for the current session
[user#server lib]$ CDPATH=.:/var:/home/user/scripts
[user#server lib]$ cd log
/var/log
[user#server log]$ cd lib
/var/lib
[user#server lib]$ cd favorite
/home/user/scripts/favorite
If you want use tab while execute cd you can install bash-completion http://bash-completion.alioth.debian.org/ but it's optional
Also do not forget cd - command for quick switching to previous working dir

You can always add the directory path in ~/.bashrc
vi ~/.bashrc
export FAV_DIR1=''
The variables in .bashrc load into the environment on new session. So make sure to reboot.
Then you can visit the directory by something like:
cd $FAV_DIR1

Related

Navigating directories when you don't know the name of the directory

I have a directory that is created through an external process. The directory is named 2021-12-08_1345 (YYYY-MM-DD_HHMM) based on the date and time when the process is executed. While this is the only directory in the path, I won't know the precise name of the directory. Is there a way to navigate to this folder knowing that it's the first and only directory?
The solution is cd $(ls -d -1 */ |sed -n '1p') where 1p is the nth directory that you want to navigate to. I came across the solution on an Ubuntu StackExchange https://askubuntu.com/questions/454688/how-do-you-cd-into-the-first-available-folder-without-typing-out-the-name#comment1800653_455113
I verified that this works on macOS 11.5.2+
If you are sure it is the only directory you can use
cd */
or
cd /path/to/*/
but this will fail if there is more than one directory.
Otherwise I suggest to use the solution from Ed Knittel's answer.
If you know that there is only one directory and no files you can even omit the trailing / from these commands, e.g. cd *.

Add new directory within subdirectory using CVS

How does one add a new directory within a subdirectory using a CVS repository?
cvs add [new_dir_name]
simply creates a new directory on the first level of the repository, while going into the subdirecory I am interested in and adding does not work. i.e.L
cd repository/directory
cvs add [new_dir_name]
Produces an error:
cvs [add aborted]: there is no version here; do 'cvs checkout' first
(This error message, though, still happens when I check out the repository).
Any ideas how to do this?
cd repository
mkdir a
mkdir a/b
mdkdir a/b/c
cvs update -d a/b/c ( not sure if in one go works, if not, try one after another)
The option -d will create directories that are missing. The same should work for add, if you cvs update afterwards and commit to persist it.
Personally I would use git or svn - changed from cvs ~10y ago
You must add each directory in the path to the final subdirectory that is not present on the server in descending oder.
For example...
If you are in the root of your cvs repository, the following should work.
mkdir -p dirname/subdirname
cvs add dirname
cvs add dirname/subdirname
alternately / equivalently
mkdir -p dirname/subdirname
cvs add dirname
cd dirname
cvs add subdirname

Symlink dotfiles

I am having trouble symlinking dotfiles. I have a folder in my home directory ~/dotfiles which I have synced to a github repo. I am trying to take my .vimrc file in ~/dotfiles/.vimrc and create a symbolic link to put it at ~/.vimrc. To do this I type in
ln -s ~/dotfiles/.vimrc ~/.vimrc
But when I run that it says
ln: /Users/me/.vimrc: File exists
What am I doing wrong?
That error message means that you already have a file at ~/.vimrc, which ln is refusing to overwrite. Either delete the ~/.vimrc and run ln again or let ln delete it for you by passing the -f option:
ln -s -f ~/dotfiles/.vimrc ~/.vimrc
There is a better solution for managing dotfiles without using symlinks or any other tool, just a git repo initialized with --bare.
A bare repository is special in a way that they omit working directory, so you can create your repo anywhere and set the --work-tree=$HOME then you don't need to do any work to maintain it.
Approach
first thing to do is, create a bare repo
git init --bare $HOME/.dotfiles
To use this bare repo, you need to specify --git-dir=$HOME/.dotfiles/ and --work-tree=$HOME, better is to create an alias
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME
At this point, all your configuration files are being tracked, and you can easily use the newly registered dotfiles command to manage the repository, ex :-
# to check the status of the tracked and untracked files
dotfiles status
# to add a file
dotfiles commit .tmux.conf -m ".tmux.conf added"
# push new files or changes to the github
dotfiles push origin main
I also use this way to sync and store my dotfiles, see my dotfiles repository and can read at Storing dotfiles with Git where I wrote about managing for multiple devices.
How to symlink all dotfiles in a directory recursively
Have a dotfiles directory that is structured as to how they should be structured at $HOME
dotfiles_home=~/dotfiles/home # for example
cp -rsf "$dotfiles_home"/. ~
-r: Recursive, create the necessary directory for each file
-s: Create symlinks instead of copying
-f: Overwrite existing files (previously created symlinks, default .bashrc, etc)
/.: Make sure cp "copy" the contents of home instead of the home directory itself.
Tips
Just like ln, if you want no headache or drama, use an absolute path for the first argument like the example above.
Note
This only works with GNU cp (preinstalled in Ubuntu), not POSIX cp. Check your man cp, you can install GNU coreutils if needed.
Thanks
To this and this.

Find and Locate Current .profile

How can I find and/or locate the file and directory of the currently loaded .profile in a bash shell. I made modifications to the current .profile on my machine, and would like to update it some more. I had created the current version in vi and couldn't tell where it was creating the file.
I am hoping that there is a command similar to "whoami"...
Bash executes startup scripts in the following order if you start a login shell:
/etc/profile
.bash_profile under you home directory
.bash_login under you home directory
.profile under you home directory
And .bashrc under your home dir in case of non-login shell.
So, probably, you just have to run vi ~/.profile

Unix: Getting Export PATH to "Stick"

When setting the export path in Unix, example:
export PATH=$PATH: $EC2_HOME/bin
If I quit terminal and open it back up to continue working, I have to go through all the steps again, setting up the paths each time.
I'm wondering how I can set the path and have it "stick" so my system knows where to find everything the next time I open terminal without having to do it all over again.
Thanks!
Open ~/.bashrc. This file is loaded every time you start up a new shell (if you're using Bash, which most people are). If you're using a different shell, the file may have a different name, like ~/.shrc.
Add the line you need to the bottom of the file:
export PATH=$PATH:$EC2_HOME/bi
Other info rolled up from elsewhere in the thread:
There are multiple places to put this, depending on your shell and your needs. All of these files are in your home directory:
For Bash:
.bashrc (executed when you shart a shell)
OR
.bash_profile (executed when you log in)
For csh and tcsh:
.cshrc
For sh and ksh:
.profile
Add it to your .cshrc file (for csh and tcsh), .profile file (for sh and ksh), or .bash_profile file (for bash)
You need to find your profile file and put that line in there. Suppose you use bash, the profile files are .bashrc and .bash_profile, found in ~. These files will vary depending on which shell you use.
You have to put those commands into one of the "autostart" files of your shell.
For bash this would be .bashrc in your homedirectory (create it if necessary)
add it to your .bashrc or another .bash startup file.
... and for ksh edit .profile.

Resources