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
Related
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.
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
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 9 years ago.
Improve this question
I want to remove all files that exist in folder new-files from another folder in linux using bash commands.
I need this for two things:
I got some setup scripts which copy some pre-configured config files over. I would like to have the option to remove those files again
Sometimes it happens that archives get unpacked into the root of your downloads directory and not into a subdir because the person packing the file put everything to the archives root
What's the best way to do that?
Edit, to clarify:
I got a folder with files called new-files.
Now I execute cp -r new-files/* other-directory/.
Lets say other-directory is not the directory I wanted to copy them to but it already contains other files so I can't just do rm other-directory/*.
I need to delete all folders which I accidently copied. How do I do that?
You could use the following command:
cd new-files ; find . -exec rm -rf path/to/other-directory/{} \;
It will list all the files that where copied from the new-files directory (new-files directory will not be taken in consideration). For each file, it will remove the copied version in other-directory.
But you've to be careful, if a file in new-files erase a file in other-directory, you won't be able to restore the old file using this method. You should consider to use a versioning system (like Git for example).
From your:
Edit, to clarify:
I got a folder with files called new-files.
Now I execute cp -r new-files/* other-directory/.
Lets say other-directory is not the directory I wanted to copy them to but it already contains other files so I can't just do rm
other-directory/*.
I need to delete all folders which I accidently copied. How do I do that?
You can loop through the original dir new-files/ and delete files with same name in the other-directory/:
for file in /new-files/*
do
rm /other-directory/"$file"
done
wee script to do what you want:
pushd `pwd`
cd /path/to/new-files
x=`find . -type f`
popd
echo $x | xargs rm
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 9 years ago.
Improve this question
I have two copies of the same directory tree. They almost have the same files in both (one version may have a couple extra or missing files). However, most of the files are in common to both directories (have the same relative paths and everything).
Assume these are in directories:
version1/
version2/
The problem is that the permissions in version1/ got messed up, and I would like to copy over the permissions from version2/, but do it without replacing the files in version1/ which are newer.
Is there an automated way to do this via bash? (It doesn't have to be bash, it could be some other method/programming language as well).
You should have a look at the --reference option for chmod:
chmod --reference version2/somefile version1/somefile
Apply find and xargs in a fitting manner and you should be fine, i.e. something like
~/version2$ find . -type f | xargs -I {} chmod --reference {} ../version1/{}
This even works recursively, and is robust against missing files in the target directory (bar the No such file ... errors, which can be ignored). Of course it won't do anything to files that only exist in the target directory.
Cheers,
You could use this script (it changes the permissions recursively but individually for each file/directory)
#!/bin/sh
chmod --reference $1 $2
if [ -d $1 ]
then
if [ "x`ls $1`" != "x" ]
then
for f in `ls $1`
do
$0 $1/$f $2/$f
done
fi
fi
Run the script with arguments version2 version1
You could try:
chmod owner-group-other ./dir or ./file
Unless permissions are fine grained and different from one file to another, you could do a recursive chmod on the directory and unify the permissions.
See man chmod for references on the options that might be useful
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
My development machine is a linux host.
I have a complicated directory structure (like most of you, I assume), and I would like to move easily from one directory to the other, from within the shell. Specifically, welcomed features would be:
autocompletion (something like ido-mode in emacs)
regular expression directory / file matching
suggestion of recently visited directories (stack).
Possibilty to push/pop to the stack, get a listing of recently visited directories, ...
good integration of those features
console based
Do you know any tool which can satisfy those requirements?
In bash you can set CDPATH to a colon-separated directories that bash will search for when the argument to the cd does not exist.
$ man bash|grep -A3 '^\s\+CDPATH '
CDPATH The search path for the cd command. This is a colon-
separated list of directories in which the shell looks
for destination directories specified by the cd com‐
mand. A sample value is ".:~:/usr".
Once set, autocomplete will just work the way you'd expect it:
$ export CDPATH=dir1:dir2
$ cd somedir<tab>
Besides the current directory, bash will look into the directories in $CDPATH for the possible values.
Umm, any interactive shell(say, bash) already has nearly all of these features:
Press Tab once to auto-complete, and twice to show a list of possible completions.
find | grep reg.exp can be used for file matching, or find -exec grep reg.exp -H '{}' ';' to match contents
You can switch to the previous directory with cd -
pushd and popd can be used to push and pop directories