Copy file permissions, but not files [closed] - linux

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

Related

Linux backup files command [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
I had a problem with my Ubuntu install. I was able to boot from liveCD and connect an external hard drive. I want to backup my files now.
I tried cp -r /home destination, but I get problem with spaces in filenames, symlinks, errors "Cannot create fifo: Operation not permitted" "Permission denied" "Invalid argument" and plenty more. What is the best way to do it? Will cp -a fix these issues or should I do something more clever?
I found out that rsync doesn't have problems with filenames. But it doesn't copy .so and .a files. Also it is running extremely slow comparing to cp.
EDIT:
I followed the advice of John Bollinger and created an archive, because my external drive wasn't ext4 formatted, so is not able to preserve all file attributes.
From a liveCD home refers to liveCD home, so one has to use:
tar -c -z -f /my/backup/disk/home.tar.gz -C / media/ubuntu/longDeviceName/home
Despite sudo, I still received some "Cannot open: Permission denied" and "socket ignored" errors creating a tar for several .png files in .cache/software-center/icons/blabla. I wonder whether it is normal.
If you do not want to reformat your backup disk with a filesystem that has enough capabilities to represent all of the attributes of your files (e.g. ext4) then preserving them across the backup requires putting them into some sort of container. The traditional container for this sort of thing is a [compressed] tarball. You might therefore try
tar -c -z -f /my/backup/disk/home.tar.gz -C / home
You would recover the contents of that tarball via
tar -x -z -f /my/backup/disk/home.tar.gz -C /
Either or both might need to be run with privilege, obtained by being root or by using sudo.
That will handle symlinks, executable files, and any filename just fine, but it may still have trouble if the data you are trying to back up include any special files, such as device nodes or FIFOs. In that event, you may simply need to remove such files first, and recreate them after restoring the other files. You can identify such files via find:
find /home -not -type f -not -type d -not -type l
The accepted answer does not backup / recover file permission.
You should use parameter "p" while backing up and while recovering.
Also you might want to recover to specific folder and then move things around to not overwrite files you might want to keep.
"/" on the end of the command stands for backing up entire system:
sudo tar -cvpzf /backupfolder/backup.tar.gz --exclude=/mnt /
sudo mkdir /recover_v1.1
sudo tar -xvpzf backup.tar.gz -C /recover_v1.1
... // replacing whatever you need manually
Manually replace files you need to recover and keep those you want to keep.
-x extract
-p include permissions
-v verbose will show you the files name while working
-z compression
-f name the file
You might want to setup cron jobs to run backup automatically.

mv command creates directories [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 am facing a very weird issue.
Consider an example I have these directories "/ten" and "/one/two/three/four" I have a few files in these directories.
When i execute the following command
mv /ten/ /one/two/three/four/five/six
it gives the output as
mv: cannot move '/ten/' to '/one/two/three/four/five/six' : No such file or directory. Which looks fine as it doesn't create directories.
But if I execute the following command
mv /one/two/three/four/ /one/two/five/six
the directories five/six get created inside /one/two. i.e. the mv command succeeds.
Can anyone please explain what is happening here ? Why doesn't it give an error No such file or directory ?
EDIT : Further Observation ..
Directories /one/two/three/four exists also directories /one/two/five exists.
Executing mv /one/two/three/four/ /one/two/five/six will succeed. Here directory six will get created even though it is not present.
This doesn't happen in the case when I execute mv /one/two/three/four /one/two/five/six and the "five" directory doesn't exists. In this case it will give error.
I thought mv will never create any directories.
Please let me know if I have missed something obvious.
Either you're executing another mv binary, executing another version of mv, or something is wrapping it up like a function, a script or perhaps an alias.
To know if you're really running the real mv or not, run
type mv
You should get
mv is /bin/mv
As suggested by Etan Reisner, you can also add -a to have more information:
type -a mv
UPDATE
Directories /one/two/three/four exists also directories /one/two/five
exists. Executing mv /one/two/three/four/ /one/two/five/six will
succeed. Here directory six will get created even though it is not
present. This doesn't happen in the case when I execute mv
/one/two/three/four /one/two/five/six and the "five" directory doesn't
exists. In this case it will give error.
Since /one/two/five existed it simply moved your directory /one/two/three/four as /one/two/five/six. That means /one/two/five/six is now the new name or pathname of the directory which was previously /one/two/three/four.
The problem in understanding you are having can be helped with a reference to the man page for mv and a few examples. From man 1 mv Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. What is not apparent is what is SOURCE and what is DEST and this is where your confusion arises. For example:
mv /ten/ /one/two/three/four/five/six
it gives the output as mv: cannot move '/ten/' to '/one/two/three/four/five/six' :
No such file or directory. Which looks fine as it doesn't create directories.
It doesn't. In you example, the SOURCE is /ten and, your DEST depends on whether /one/two/three/four/five exists and also whether /one/two/three/four/five/six exists.
If /one/two/three/four/five exists, then mv /ten /one/two/three/four/five will cause /ten to be moved and become a new subdirectory of /one/two/three/four/five. e.g. /one/two/three/four/five/ten.
If /one/two/three/four/five exists (but not ../six), then mv /ten /one/two/three/four/five/six will cause /ten to be moved and become six new subdirectory of /one/two/three/four/five. e.g. /one/two/three/four/five/six.
if however /one/two/three/four/five do not exist, then mv will fail because you have not provided a valid DEST.

Linux chown -R parameter, what does it 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 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.

linux bash - remove all files which are in one directory from another directory [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 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

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

Resources