Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
The explanation is:
"-R, --recursive
operate on files and directories recursively"
What does "recursive" mean here?
"Recursive" implies that the operation will be performed for all files and directories (and all files and directories within any directory). So
chown -R foo /some/path
would change file owner to foo for all files and directories in /some/path
p.s. You might have even seen the dictionary entry for recursive:
recursive, n: See recursive
In some Linux commands, if you run the command on a folder with -R, the command will operate on all files and folders in that folder's tree. If you run the command on a file, -R has no effect.
The command will operate on given folder, and recursively operates on files and folders within it. It is based on recursion.
For example, you can remove a folder and its contents with
rm -R folder-name
Or you can find all occurrences of a specific string in all files within current folder tree with
grep -R -n the-string .
In this example -n is for displaying line numbers.
It means apply it to sub-directories and their contents, that is, recurse chown() when a directory is encountered.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I often find myself wanting to copy files from one of the directories in my directory stack to another, and the best solution I've come up with is cp $(dirs -p | tail -n 1)/somefile.txt ./somefile.txt
Is there a better solution for this?
You can access the directory stack directly via the DIRSTACK array; no command substitutions are needed.
cp "${DIRSTACK[-1]}/somefile.txt" .
Your original code was buggy for the same reason the output of ls should not be used programmatically; a directory name containing a newline character could break it. For example:
$ pushd $'foo\nbar'
$ dirs -p | head -1
~/bin/foo
Using dirs +0 at least fixes that problem (assuming you correctly quote the command substitution as cp "$(dirs +N)"/somefile.txt ./somefile.txt, anyway):
$ dirs +0
~/bin/foo
bar
After referncing the https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html#Directory-Stack-Builtins, a slightly better solution is cp $(dirs +N)/somefile.txt ./somefile.txt
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
when i copy files, execute the file in linux, I am Not quite understand the difference and how to use them.
please help. thanks.
. means current directory
./ means current directory, too
./* means all files in current directory
. means the current directory, and ./ is the same but more explicit, saying "Hey, I'm a directory!" It's like any other folder: TheFolder and TheFolder/ refer to the same object. One case where the meaning is different is when looking at a symlink pointing at a directory: TheLink can refer either to the link object or the directory that you pointed at, depending on the situation, while TheLink/ will always refer to the directory. Also, when you run the rsync command, it will treat TheFolder and TheFolder/ differently.
./* just means all the files in the current directory, same as *. Bash expands that asterisk before the command is run, so the program doesn't see the asterisk, instead seeing all the files as arguments. The difference between these two is that for the former, the command will see "./" prepended to each filename.
To see how these differ, echo is a safe command to run. It will just print the value of what you send it:
echo *
echo ./*
. and ./ both mean current working directory
../ means parent directory of the current working directory
./* means all files and directories in current working directory plus all files and directories in all directories in the current working directory.
./* is all files in current directory
./ is current directory
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 10 years ago.
Improve this question
Surprisingly I could not find a straight-forward answer to this question on here yet. I am still learning Linux. Say I have downloaded a zip file to my Downloads folder. Now, I want to move it into a protected folder, like /opts or /var. Is there a good command to both sudo move AND unzip the file to where I need it to go?
If you wish to perform two separate operations (move and extract) then you have no option but to use two commands.
However, if your end goal is to extract the zip file to a specific directory, you can leave the zip file where it is and specify an extraction directory using the -d option:
sudo unzip thefile.zip -d /opt/target_dir
From the manpage:
[-d exdir]
An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed. In particular, ''-d ~'' (tilde) is expanded by Unix C shells into the name of the user's home directory, but ''-d~'' is treated as a literal subdirectory ''~'' of the current directory.
sudo mv <file_name> /opts && unzip /opts/<file_name>
Also you may specify the unzip destination to unzip so you can do this in a single command. This however will be a bit different from the command above as the zip will be kept in its current location, only the unzipped files will be extracted to the pointed destination.
unzip -d [target directory] [filename].zip
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