Make zip of directory contents and name zip same as directory - recursively - directory

I have over 500 sub directories coming off of one root directory that each contain >6000 files each. the directories are named 20150218, 20150217, etc., one for each day of the year.
I want to develop a script that will zip all of the files in a directory, i.e. 20150217 and name the directory 20150217.zip. I then want to delete the original files.
so, all of the sud directories in ~/public_html/ispy/dlink/ would be zipped separately.
I appreciate any guidance.

Copy and paste following script into any unix editor (vim, geany, mousepad) and save it in directory with Your "date" subfolders. Name it as you wish, with no extension e.g. zipscript. From terminal: go to directory with Your script, allow to execute it: chmod +x zipscript and fire it: sudo ./zipscript.
#!/bin/bash
for D in *
do
if [ -d "${D}" ]; then
zip -r -j "${D}".zip "${D}"
rm -R "${D}"
fi
done
Script runs as follows: for every file (directory is also a file under unix) in current directory check if it is a directory and, if true, make zip with the same name, then delete subdirectory with all files in it.

Related

How do I ls a directory to get it's data, but none of its files?

I wish to run ls -alh on a directory, so that I can get when the directory was last modified.
I do NOT want to get the ls information for the files in that directory, just that directory.
Is this possible?
Turn's out it's easy
ls -alhd
The -d flag reports on directories, not their files

Symlink working but is showing the directory one level up for some reason

I have a directory that contains media that I am trying to setup a basic symbolic link to - the directory is a mounted storage on a digital ocean droplet in the following directory /mnt/storage/media/all
this contains directories as shown below:
0118
0119
0218 and so on.......
I am trying to make a symlink from my unix terminal as follows :
$ root#server1:/var/www/abcd/public ln -s /mnt/storage/media/all
So if I cd into the public directory above I would expect to see the directories 0118, 0119, 0218 and so on... however when I cd into this directory I see the directory all and within this directories are the 0118, 0119, 0218 subdirectories.
How do I change the symbolic link so I see the directories 0118, 0119, 0218 etc.. and not the all directory (which contains those same sub directories)
Try giving a second argument for the function. Let that argument be the desired destination for the link (but it shouldn't exist prior to this).
E.g. ln -s /mnt/storage/media/all /var/www/abcd/public
In case the folder public already exists, the symlink will be created inside it.

MacOS: xargs cp does not copy subdirectories

I am on Mac OS.
I have a directory with round about 3000 files and several subdirectories (wordpress installation)
Now I have to find all the files in a similar directory (have to separate master and child installation) that are additional files and have to copy them away into another directory.
I use this command:
$ diff -rq dt-the7 dt-the7-master-from-Yana|grep 'Only in dt-the7'|awk {' print $3 $4 '}|sed 's/:/\//g'|xargs -J {} rsync -av {} neu/
but somehow a certain file 3d.png and a list of other that should be in a subdir of the destination dir are copied into the root dir of the destination.
Any idea why that might be?
It makes no difference whether I use cp, rsync or ditto
You need the -R relative option on your rsync command.
Without this rsync just copies the item referenced rather than the path referenced, so items at the root level are copied as you expected but items in sub-directories are also copied to the root, which is not what you wanted.
With the option rsync takes account of the relative path and recreates it at the destination.
An example with another command might help, consider:
cp A/B.txt C/
that will copy B.txt into C, it does not create a folder A in C which in turn contains the file B.txt. rsync without -R behaves like that cp command, with -R it creates the A directory in C.
HTH

How to copy a entire directory which contains symlinks?

I want to copy a complete directory content from /home/private_html/userx/ into the /home/private_html/usery/, the problem is that the directory userx contains few symlinks, and when using the cp it just skip them (skip occurs, if symlinks directs into a file, in case if it points into the directory, it just copy WHOLE directory instead...).
The command I was using looks following:
# cp -iprv /home/private_html/userx/ /home/private_html/usery/
Has anyone a solution to copy the directory "just as it is" into other place?
On FreeBSD, cp doesn't have an -r option. It does have -R, which should do what you want:
-R If source_file designates a directory, cp copies the directory and
the entire subtree connected at that point. If the source_file
ends in a /, the contents of the directory are copied rather than
the directory itself. This option also causes symbolic links to be
copied, rather than indirected through, and for cp to create spe‐
cial files rather than copying them as normal files. Created
directories have the same mode as the corresponding source direc‐
tory, unmodified by the process' umask.
Roland is right about the -R flag. You could also use a pair of tar-processes, which would make your command a little bit more system-independent:
tar -C /home/private_html/userx/ -cpf - . | tar -C /home/private_html/usery/ -epf -

Unix- Copying the same file from multiple directories into a new directory while renaming the files

I have 36 subdirectories in the same directory named 10,11,12,....45 and a subdirectory logs
in each subdirectory (except for the directory logs) there is the same file called log.lammps
i was wondering if there was a way where i could copy each log.lammps file from each subdirectory 10-45 and put it in the sub directory logs while also adding the number of the directory that it originated from to the end of the filename
so i am looking for a code that copies the file log.lammps one by one from each subdirectory and every time the file gets copied into the directory logs, the filename gets changed from log.lammps to log.lammps10 if it came from the subdirectory 10 and when the file log.laamps from subdirectory 11 is copied into logs its name changes to log.lammps11 etc.
any help would be appreciated since right now i am only dealing with 30-40 files and in time i will be working with hundreds of files
Something along this line should work:
for f in [0-9][0-9]/log.lammps; do
d=$(dirname ${f})
b=$(basename ${f})
cp ${f} logs/${b}.${d}
done
That's easy-peasy with the magic of shell scripting. I'm assuming you have bash available. Create a new file in the directory that contains these subdirectories; name it something like copy_logs.sh. Copy-paste the following text into it:
#!/bin/bash
# copy_logs.sh
# Copies all files named log.lammps from all subdirectories of this
# directory, except logs/, into subdirectory logs/, while appending the name
# of the originating directory. For example, if this directory includes
# subdirectories 1/, 2/, foo/, and logs/, and each of those directories
# (except for logs/) contains a file named log.lammps, then after the
# execution of this script, the new file log.lammps.1, log.lammps.2, and
# log.lammps.foo will have been added to logs/. NOTE: any existing files
# with those names in will be overwritten.
DIRNAMES=$( find . -type d | grep -v logs | sed 's/\.//g' | sed 's/\///g' | sort )
for dirname in $( echo $DIRNAMES )
do
cp -f $dirname/foo.txt logs/foo$dirname
echo "Copied file $dirname/foo.txt to logs/foo.$dirname"
done
See the script's comments for what it does. After you've saved the file, you need to make it executable by commanding chmod a+x copy_logs.sh on the command line. After this, you can execute it by typing ./copy_logs.sh on the command line while your working directory is the directory that contains the script and the subdirectories. If you add that directory to your $PATH variable, you can command copy_logs.sh no matter what your working directory is.
(I tested the script with GNU bash v4.2.24, so it should work.)
For more on bash shell scripting, see any number of books or internet sites; you might start with the Advanced Bash-Scripting Guide.

Resources