Change cd default directory (bash) [closed] - linux

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 8 years ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm looking for a way to change the default directory of cd, and I'm wondering if this is possible. I tried adding
alias "cd=cd ~/Documents/Github"
to .bashrc, but this obviously doesn't work because it breaks the cd command so that you cannot use cd for anything but going to that directory. Is there some export that I could put in .bashrc to do this? I do not want to change my home directory, just the default directory cd goes to. I ask because I regularly use cd to change to my default directory that I am programming in and would like to not have to type cd ~/workspace or cd and then cd workspace every time I want to change to the directory ~/workspace.

Here's a simpler alternative using an alias:
alias cd='HOME=~/Documents/Github cd'
While this redefines $HOME, it does so ONLY for the cd command, so should be safe(*).
This also obviates the need for any custom parameter pre-parsing.
If you place this in ~/.bashrc, you should get the desired behavior.
Note that, by default, this alias will NOT be in effect in scripts (non-interactive shells), as alias expansion is by default disabled there (use shopt -s expand_aliases to explicitly enable).
(*) #chepner points out one restriction: with the alias in place you won't be able to do HOME=/somewhere/else cd, i.e., you won't be able to redefine (override) $HOME again, ad-hoc. As he further states, though, it's hard to imagine why anyone would want to do that.

You can shadow the builtin cd command with a shell function. Add the following to your .bashrc file.
cd () {
if [ $# = 0 ]; then
builtin cd ~/Documents/Github
else
builtin cd "$#"
fi
}
This isn't perfect, as it assumes you will never call the function as
* cd -L
* cd -P
* cd -LP
* etc
(that is, using one or more of the supported options without an explicit directory.)
UPDATE
This might be a little more comprehensive, but I haven't tested it.
cd () {
local -a args
while getopts LP opt "$#"; do
case $opt in
-*) args+=( "$opt" ) ;;
esac
done
shift $((OPTIND-1))
# Assume at most one directory argument, since
# that is all cd uses
args+=( "${1:-~/Documents/Github}" )
builtin cd "${args[#]}"
}

this is default home location
$ pwd
/home/######
now you can reset it like this
$ export HOME=/tmp
$ cd
$ pwd
/tmp
now it's up to you where to put the new $HOME definition - .bashrc, or whatever

Related

How to reset bash config? [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 2 years ago.
Improve this question
Previously, when I mistakenly entered a wrong command in the terminal, I would get an output like this:
Command 'whoaim' not found, did you mean:
command 'whoami' from deb coreutils (8.30-3ubuntu2)
Try: sudo apt install <deb name>
but now when I enter a wrong command like whoaim instead of whoami I get this output:
bash: whoaim: command not found
how should I reset bash config?
You can reset the bash profile of user using following commands:-
Overwrite the existing .bashrc from user's home directory.
cp /etc/skel/.bashrc ~/
source ~/.bashrc
Take backup of existing .bashrc from user's home directory
However, check here how you can use the utility command-not-found in ubuntu
You would need to call a function that gives bash this capability. It's typically called command_not_found_handle. First, append it to your .bashrc file. Here is the code:
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1";
return $?;
else
if [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1";
return $?;
else
printf "%s: command not found\n" "$1" 1>&2;
return 127;
fi;
fi
}
And then run this command to reload the profile file:
. ~/.bashrc

What does the command cd . mean? [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 6 years ago.
Improve this question
I understand that cd .. goes up a directory, but what exactly does cd . do?
This is a question on my study guide, which is why I am asking.
Thanks!
As most people realize, cd . doesn't really do anything, because . means "the current directory". Changing to the current directory is seemingly pointless.
However, there is at least one interesting side effect:
When you cd to some directory, an environment variable OLDPWD is set, which allows you to execute cd - to return to the previous directory that you cd'ed from. When you execute cd ., OLDPWD actually gets set as the current directory, so it renders cd - ineffective in getting you back to the previous directory you were in.
Example:
$ cd /foo
$ cd /tmp
$ cd -
$ pwd
/foo
I'm in /foo just like I expected, but
$ cd /foo
$ cd /tmp
$ cd . # does nothing, right?
$ cd -
$ pwd
/tmp
Now I didn't return to /foo like I was hoping, due to this side effect!
Nothing. cd changes directory to the argument provided. And . means "the current directory" just as .. means "the parent of the current directory".
As others have said, cd . will change to the current directory, which has basically no effect in scripting/programming or to the operating system.
However that doesn't mean it will do nothing. In practice, you may be using a terminal to type this command. It will happily execute the command and - perhaps, though not likely - do something like echo the contents of the directory. If something else on your system (say a background process or another shell) have changed the contents of the directory, you may see modified output when the command echoes out the new directory listing.
In short: the cd . command doesn't have any real effect, but it could - in rare instances - have useful side effects that you could leverage in practical use.

linux "lessPATH" similar to $CDPATH and $PATH [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 8 years ago.
Improve this question
I like the flexibility and how easy it is to execute commands from any directory in $PATH variable or $CDPATH to change directory.
But is there an easy way to "less or open a file" from a list of directories you often visit.
Say you have log files in a different dir and other frequently visited directories in another. A less file1 command should look for the file1 in a list of directories defined like a $PATH or $CDPATH variable.
I understand that you look for an existing solution but if you use bash you can write a function than has exactly this behavior :
put in ~/.bashrc:
less2()
{
if [ $# -eq 0 ]; then
echo 'Missing filename ("less --help" for help)'
return 1
fi
if [ "$1" == "--help" ]
then
less $1
return 1
fi
OLDIFS=$IFS
IFS=':'
if [ -z $LESSPATH ]; then
SEARCH_PATHS=.
else
SEARCH_PATHS=.:${LESSPATH}
fi
for dir in $SEARCH_PATHS
do
if test -e "$dir/$1"
then
less "$dir/$1"
IFS=$OLDIFS
return
fi
done
IFS=$OLDIFS
echo "$1: No such file or directory"
}
You need to execute source ~/.bashrc in order to get less2 in your bash.
How to use the script
By defaults it looks for files in a current directory. If you set the enviroment variable LESSPATH less2 will look for a file first in the current directory and then if it is not there it will look for the file in all the directories in $LESSPATH. less2 is a function in a current bash process so it is not necessary to export LESSPATH, but of course you can also export LESSPATH.
$ less2 my_file.log
$ LESSPATH=path1:path2:path3
$ less2 my_other_file.log

list a linux directory after erasing it [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 11 years ago.
Improve this question
I'm wondering if it's possible to list the content of a directory in linux after erasing it and recreating.
Explanation: I'm on a terminal in this particular directory. From another terminal, I erase it and recreate it and put some content inside. If I list this directory from the first terminal, it appears as empty. I need to cd .. and enter inside again to list it's content.
Is there another method who doesn't need to do that?
The only way, as far as I know, is to cd in it again. However, you can do it with one simple command. Select which one you like the most between the following four
cd ${PWD}
cd $PWD
cd $(pwd)
cd `pwd`
You can also add to your ~/.bashrc an alias like this:
alias refresh_dir="cd \$PWD"
and then call the refresh_dir command directly
Would ls ../{directory-name} work? So if the directory was called "test", and you were inside it, you'd use the command ls ../test/.
Nope. You can, of course,
cd "$PWD"
which is a quicker way in a sense.
Some shells might not cache the inode for the current working directory, but I have a sneaking suspicion that POSIX might require shells to. If you think about it, having the shell do this automatically might result in unintended loss of data (because a program/script might go and modify stuff in a directory that wasn't strictly it's working directory to begin with).
Also look at bash PROMPT_COMMAND for a hint on how to automate it, of you find yourself having to type cd "$PWD" more than you like
terminal 1:
mkdir dir/
cd dir/
touch foo/
ls
terminal 2:
rm -r dir/
mkdir dir/
touch dir/bar
terminal 1:
cd `pwd`
ls

Display Directories with a Bold Font – How to enable? with .bash_profile? [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 1 year ago.
Improve this question
I'm using a linux server that display directories in a bold font, and files in a normal font.
e.g. $ ls produces
afile.txt afolder anotherfile.txt anotherfolder
I'd like to use this feature on some other servers. How can it be done? with the .bash_profile?
If anyone has other ideas on how to differentiate folders from file, they'd be good to know?
You need to give ls the --colors=… option (e.g. via an alias). To actually configure the LS_COLORS environmental variable used to define the colours, one good way is to create a configuration file for dircolors, e.g. with just bold (attribute 1) directories:
echo DIR 1 >~/.dir_colors
Then in your .bash_profile or .bashrc, eval the output of dircolors run on that file to set LS_COLORS according to your configuration. The relevant lines in my .bashrc (copied from somewhere) look like this:
if [ -n "$COLORTERM" ]; then
alias ls='ls -F --color=auto'
if [ -x "`which dircolors`" -a -r "$HOME/.dir_colors" ]; then
eval `dircolors -b "$HOME/.dir_colors"`
fi
else
alias ls='ls -F'
fi
Note that some terminals do not, by default, display the bold attribute as true bold but rather just use a brighter colour. You need to configure your terminal to get real bold.
See the dircolors --print-database for an example of a “complete” configuration file.
add this to your .bashrc or .profile: alias ls='ls --color=auto'
If anyone has other ideas on how to differentiate folders from file, they'd be good to know?
ls does have the ability to differentiate items using the -F flag.
From the man page:
-F, --classify
append indicator (one of */=>#|) to entries
All you have to do is add -F to your ls command in your terminal like so:
$ ls -F
You can make this permanent by setting up an alias by opening your .bashrc (or similar) file and adding:
alias ls='ls -F'
After setting that (and either logging out and back on or running source ~/.bashrc), you can simply run ls and it will always assume you added -F.
See the man page for ls, the -F option as was said above gives you probably what you need to readily see which names are folders, executables and symbolic links. And see the -G option to color the files you can also use both. I guess it's so simple, I wouldn't bother to put it in an alias. You can also use both to get the two effects at the same time.
ls -F -G
ls -FG
It's done with an environment variable. Start with reading the ls man page:
$ man ls
You may not be able to get just bold, but only bold with a specific color.

Resources