move many directories to another directory using CLI - directory

I want to copy some of the directories in a directory to another directory in that directory itself.
For Example:
I have the structure like the below
myname#compname:~/root/app/pages$ ls
dir1, dir2, dir3
I want the dir1 and dir2 to be moved into dir3 and the final structure has to be as the following:
myname#compname:~/root/app/pages$ ls
dir3
myname#compname:~/root/app/pages/dir3$ ls
dir1, dir2
I know the there are many shell programs for it. but I want this to happen using the CLI itself in just a single lined statement.

mv dir1 dir2 dir3
will do it
mv dir1 dir2
mv dir2 dir3
does the same thing, in two steps.
Technically mv dir[123] would also do the same thing (for these file names), but would potentially be confusing as the destination is determined alphabetically

This is not the perfect answer. But i have done the above with the following code:
myname#compname:~/root/app/pages$ cp -r dir1/ dir3/ && cp -r dir2/ dir3/
myname#compname:~/root/app/pages$ rm -r dir1 && rm -r dir2
myname#compname:~/root/app/pages$ cd dir3 && ls
Other Answers are welcome!

Related

Unix Command to delete siblings and parent directory of a given file

I have a directory structure like this
/home
/dir-1
some-file.php
/dir-2
sibling.php
target-file.php
/dir-3
/dir-4
other-sibling.php
sibling.php
target-file.php
/dir-5
target-file.php
I need to target all directories containing the file "target-file.php" and remove those directories with its contents. In my structure, the final result wanted is:
/home
/dir-1
some-file.php
/dir-3
I am trying:
rm -rf /home/*/target-file.php
But it is only removing that file (target-file.php) and not the siblings or the parent directory.
Please help
Use this:
#!/bin/bash
find . -type f -name target-file.php -print0 | while IFS= read -r -d '' line
do
echo "$line"
/bin/rm -fr "$(dirname "$line")"
done
Using find with while like this ensure it will work with all filenames (see https://mywiki.wooledge.org/BashFAQ/001).
You can run find . -type f -name target-file.php -print to see the list of files.
dirname removes the filename so you are left with only the directory names.
/bin/rm -fr deletes the directories.
you can comment the echo line, this was just to show you the files / directories being processed.

Exclude files from tar gzipping a directory in unix

I have a directory (dir) (with files and subdirectories):
ls -1 dir
plot.pdf
subdir.1
subdir.2
obj.RDS
And then ls -1 for either subdir.1 or subdir.2:
plot.pdf
PC.pdf
results.csv
de.pdf
de.csv
de.RDS
I would like to tar and gzip dir (in unix) and I'd like to exclude all RDS files (the the level right below dir and the ones in its subdirectories).
What's the easiest way to achieve that? Perhaps in a one liner
Something like:
find dir -type f -not -name '*.RDS' -print0 |
tar --null -T- -czf TARGET.tgz
should do it.
First, find finds the files, and then tar accepts the list via -T- (= --files-from /dev/stdin).
-print0 on find combined wth --null on tar protect from weird filenames.
-czf == Create gZipped File
You can add v to get verbose output.
To later inspect the contents, you can do:
tar tf TARGET.tgz
tar --exclude=*.RDS -Jcf outputball.tar dir_to_compress
this will ignore *.RDS across any dir or subdirs
decompress using
tar -xvf outputball.tar

Unix Copy Recursive Including All Directories

I have the following two directories:
~/A
drawable/
imageb.png
new/`
newimage.png
~/B
drawable/
imagec.png
When I use the cp -r ~/A/* ~/B command newimage.png with its new/ folder is copied across to ~/B however imageb.png is not copied into ~/B/drawable.
Could you explain why this is the case and how I can get around this?
Use tar instead of cp:
(cd A ; tar cf - *) | (cd B ; tar xf -)
or more compactly (if you're using GNU tar):
tar cC A -f - . | tar xC B -f -
If you are on linux you can use the -r option.
eg: cp -r ~/A/. ~/B/
If you are on BSD you could use the -R option.
eg: cp -R ~/A/. ~/B/
For more information on exactly what option you should pass, refer man cp
Also note that, if you do not have permissions to the file you it would prevent copying files.

How to copy a file from one directory to another directory in UNIX

Ex: I have 2 directories
dir1
file1.txt
dir2
I need to copy file1.txt into dir2.
Note: dir2 is not a sub-directory of dir1. both are unique directories.
cp dir1/file1.txt dir2
or if you are in dir1
cp file1.txt ../dir2
cp dir1/file1.txt dir2/. -v
-v Verbosiry

How to recursively diff without transversing filesystems?

I'd like to create a patchset for two directory trees both of which contain (bind-)mounts which should be ignored. Is there any diff -r option similar to rsync's -x, --one-file-system? Or is another tool more appropriate for this? I considered using rsync --compare-dest, but the problem is a "diff"-directory obtained this way contains no information on file deletions.
Background: I want to store the modifications made to a chrooted-into Gentoo stage3 archive
As a workaround, I currently waste a lot of time by running rsync twice:
ORIG=/path/to/original
MOD=/path/to/modded
# find the modified/added files:
mkdir modded && rsync -axP --prune-empty-dirs --compare-dest=$ORIG $MOD/ modded
# the other way around, includes both deleted and modded files
mkdir deleted && rsync -axP --prune-empty-dirs --compare-dest=$MOD $ORIG/ deleted
# find the modded files and remove them
for i in $(find deleted); do [ -e modded${i#deleted} ] && rm $i; done
# delete the empty directories
find modded delete -type d -empty -delete
# create a list of the deleted files
cd deleted && find -type f > ../deleted.list && cd ..
# tar the modifications
cd modded && tar czf ../modded.tgz && cd ..
rm -rf deleted modded
Now modded.tgz contains the files that were modified/added, while deleted.list contains the names of deleted files, so to apply them run
tar xf modded.tgz
while read -r line; do rm $line; done < deleted.list
This can probably also be used to create a patchfile instead...

Resources