Copying a Directory Tree File by File - bash

My advisor frequently compiles a bunch of software into his /usr/local and then distributes his /usr/local as a tarball. I then extract the tarball onto our lab machines for our use. Until now, this has worked fine, we just replace the old /usr/local with the contents of the tarball.
But I have started installing my own software on these machines. If I delete /usr/local, obviously some of that software and config gets deleted as well.
Instead, how can I recursively copy files from the tarball into the corresponding directory in /usr/local?
For example:
tarball path filesystem path
------------ ---------------
local/bin/myprog -> /usr/local/bin/myprog
local/lib/mylib.so -> /usr/local/lib/mylib.so
etc.
Or is this a better question for ServerFault?

$ cd /usr
$ tar xvf f.tar
or
$ cd /tmp
$ tar xvf f.tar
$ cp -R local/. /usr/local/.
Although really, I think it should just go in some other directory, or in a subdir of /usr/local/. There isn't anything magical about /usr/local/ except perhaps a default PATH component.

The cp command has the -r flag for copying recursively:
$ cp -r local/* /usr/local/
Please look up your system's man page for cp for more information.

Use the k option and specify the destination to protect against overwriting files:
$ cd /usr
$ tar xvfk localtarball.tar local
local/
local/file
tar: local/file: Cannot open: File exists
local/bar2/
local/bar2/bar3
tar: local/file: Cannot open: File exists
local/bar2a/bar2aY/
tar: Error exit delayed from previous errors

Related

Can't open Miranda programming language tar ball Mac El Capitan

I'm trying to:
sudo tar xpzf mira-2044-x86_64-Darwin.tgz -C /
I am in the correct directory (my Downloads folder) but I get this error:
./usr/: Can't set user=0/group=0 for usrCan't update time for usr
tar: Error exit delayed from previous errors.
What does this error mean in this context?
Option p of tar means preserve permissions.
tar is attempting to set file permission, ownership and modification time of some existing directories. In MacOS, /usr is owned by root:wheel but the archive has it owned by root:root.
Unfortunately, when running tar as superuser, the default is to preserve permissions. So you cannot turn this off.
Fortunately, k option of tar will skip overwriting existing files or directories. You can tell tar not to overwrite /usr, /usr/local, /usr/local/bin and any directories that you already have.
Modification time is always restored when a file or directory is restored. Option k will already prevent modification time to get updated when it tells tar not to overwrite existing directories/files. It is still better to know that the option m tells tar not to update the modification time of a file/directory.
You should now be able to install miranda by running:
sudo tar xmpkvzf mira-2044-x86_64-Darwin.tgz -C /
That added v (verbose) option is just to let you see what are being extracted.

Cannot run ctags after executing "ln -s /usr/bin/ctags-exuberant"

By mistake I have executed the following command on my redhat office machine and now I cannot run ctags -R * any more.
ln -s /usr/bin/ctags-exuberant
Hope following info helps,
[ypp:~]$ ls /usr/bin/ctags-exuberant
[ypp:~]$ ls: /usr/bin/ctags-exuberant: No such file or directory
[ypp:~]$ unlink /usr/bin/ctags-exuberant
unlink: cannot unlink /usr/bin/ctags-exuberant': No such file or directory
[ypp:~]$ which ctags
alias ctags='/usr/bin/ctags'
/usr/bin/ctags
[ypp:~]$ ln -s /usr/bin/ctags-exuberant /usr/bin/ctags
ln: creating symbolic link '/usr/bin/ctags' to '/usr/bin/ctags-exuberant': File exists
[ypp:~]$ ln -s /usr/local/bin/ctags /usr/bin/ctags-exuberant
ln: creating symbolic link ,/usr/bin/ctags-exuberant' to '/usr/local/bin/ctags': Permission denied
So far I have tried,
[ypp:~]$ rm /usr/bin/ctags-exuberant
/bin/rm: cannot lstat /usr/bin/ctags-exuberant': No such file or directory
[ypp:~]$ rm -i /usr/bin/ctags-exuberant
/bin/rm: cannot lstat /usr/bin/ctags-exuberant': No such file or directory
[ypp:~]$ unlink /usr/bin/ctags
unlink: cannot unlink /usr/bin/ctags': Permission denied
Please tell me how do I get my ctags working back to normal,
I don't have superuser privilege also.
According the manpage, ln creates a link to file TARGET with the name LINKNAME. If LINKNAME is omitted, as was your case, a link to TARGET is created in the current directory, using the name of TARGET as the LINKNAME. As a result, when you executed ln -s /usr/bin/ctags-exuberant a symbolic link (sometimes called soft link) named ctags-exuberant would have been created in your present working directory pointed at /usr/bin/ctags-exuberant.
There seems to be more at play than what you have shared with us here. Simply creating a link to ctags-exuberant in any directory should not have resulted in the changes you are experiencing. Where there other changes made to your path?
If, somehow just creating the link has caused all your troubles, then you need to focus on finding the resulting link and removing it. If you don't know what directory it was created in, you could find it via the following:
find / -type l -name 'ctags-exuberant' 2>/dev/null
Once it is found, remove it and see if that fixes your problems. Let us know what you find.
Else, you might do better to try the wise minds over at https://unix.stackexchange.com/

Run a command with relative path from symlink directory

I've got a directory structure like this:
bin
drush
build -> vendor/drupal/core
vendor
drupal
core
So as you see, build is a symlink to the core directory.
drush is an executable file. I need to be able to cd into build and call drush from there.
But if I do this:
cd build
../bin/drush
This doesn't work, because .. points to vendor/drupal directory, because when I cd into build, it in fact goes to vendor/drupal/core, of course.
I know I can call it this way: ../../../bin/drush, but is there some kind of workaround to make .. point to the root, not the actual parent?
You can create an another symlink in the folder "drupal" on the executable drush. So with this way, you can call it from "build".
$ cd vendor/drupal/
$ ln -s absolute_path/bin/drush dummy-drush
$ cd build
$ ./dummy-drush
Solution 1 :
If you don't want to create anything in vendor, maybe it's better to create an alias which allows to you to execute drush (be careful to give the good path).
Solution 2 :
If the directory bin in your example is not the folder "/bin" from the root, you can add it in your PATH in order to execute every executable .
$ export PATH=$PATH:/personal_bin/
$ cd bin
$ ln -s drush dummy-drush

Copying from mount share drive to local folder through script

This is my first time trying to work with Linux Scripts so this may be something obvious.
Here is what I am trying to do:
Remove all contents from local folder - rm /home/user/Documents/Exercise/
Copy files from a shared windows network drive - cp smb://server/arc/Exercise%20Files/Word/
So from my understanding my command should look like this
rm /home/user/Documents/Exercise/
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
But anytime I try and run either of the above commands I get the following error:
"rm: cannot remove `/home/user/Documents/Exercise/': Is a directory"
"cp: cannot stat `smb://server/arc/Exercise%20Files/Word/': No such file or directory"
What am I doing wrong?
Kind Regards,
M
Based on your request and your test, let me point what is not written properly:
Remove all contents from local folder
rm /home/user/Documents/Exercise/
Error says rm: cannot remove /home/user/Documents/Exercise/': Is a directory
You should
rm /home/user/Documents/Exercise/*
which will delete everything inside the directory, but not the directory.
Copy files from a shared windows network drive
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
Error says cp: cannot stat smb://server/arc/Exercise%20Files/Word/': No such file or directory
You should check if route smb://server/arc/Exercise%20Files/Word/ is correct. Then, use the following:
cp smb://server/arc/Exercise%20Files/Word/* /home/user/Documents/Exercise/
You can't delete a directory if it has content within it.
To delete the content and the directory at the same time, use the following command:
rm -r /home/user/Documents/Exercise/
This recursively deletes the directory and any content within it.
To copy the file, I believe you have to mount the directory beforehand, like so:
mount -t cifs //server/share /mnt/mount_directory -o user=username
Can you confirm if that works?
Remove / Delete Command:
rm -rfv /home/user/Documents/Exercise/*
Copy Command:
cp -rfv /home/user/Documents/ExerciseShare/ExerciseFiles/Word/ /home/user/Documents/Exercise/

How can I compress a directory, and convert soft to hard links?

I would like to compress a directory.
tar -cvzf mydir.tar.gz mydir
but this retains symlinks so that it is not portable to a new system.
How can I convert symlinks?
I have tried
tar -cvzfh
since man tar says
-h, --dereference
don’t dump symlinks; dump the files they point to
but this results in an error
tar: Error exit delayed from previous errors
and creates a file called "zh"
My files are on a RHEL server.
Your tar.gz file name must follow immediately after the -f flag, merely reordering the flags may work.
tar -cvzhf mydir.tar.gz mydir

Resources