Shell - create zip files in directories without including subdirectories - linux

I have a structure like this:
file1.jpg
file2.png
subfolder1
subfolder2
file3.jpg
...
now I would like to create a zip file via shell only of the files present in the root of the directory without taking into consideration the subdirectories and the files contained within them.
How can I do? I hope I have been clear enough

You can try something like this by using a find.
It will only find files that are in the current folder and zip it to a file called filename.zip
find . -maxdepth 1 -type f -exec zip filename.zip '{}' \;

Related

How to rename multiple files, contained in multiple sub-directories

I have a parent directory that contains multiple subdirectories, and each subdirectory has multiple files. For example:
Sub-directory_1
xyz1.fq.gz
xyz2.fq.gz
Sub-directory_2
abc1.fq.gz
abc2.fq.gz
etc.
I'd like to add the subdirectory name, as a prefix, to each file in each subdirectory.
I've been trying to do this with a loop that uses subdir and FILENAME, but can't get it right. Any help is appreciated.
You can use find command:
$ find . -type f -exec bash -c 'mv {} "$(dirname {})/$(dirname {})_$(basename {})"' \;

Copy or move all files in a directory regardles of folder depth or number

Lets say i have a folder named Pictures and I want to move or copy all files out of this folder.
However I also want to move and harvest all of the files who are in sub folders so:
Pictures/1.png
Pictures/yolo/2.png
Pictures/yolo/swag/sand/3.png
Pictures/extra/fire/4.png
I want to move or copy all these files to another folder like results so I get:
results/1.png
results/2.png
results/3.png
results/4.png
Only I have no idea in advance what sub folders will be in the Pictures folder.
How can I accomplish this in bash/shell scripts ?
I also appreciate making it file type neutral so any files are harvested from their directories (not only .png like in my example) and I have no idea what the file name will be (I only used 1...4 because i did not have any idea how to name them).
You can do it like this:
find /absolute/path/to/Pictures -type f -name '*.png' -exec mv -i {} /absolute/path/to/results \;
Another option is to use xargs
find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results
You can simply copy all files and subdirectories along with their contents using cp's recursive option:
cp -pr <source_path>/* <destination_path>/
But, moving them recursively is a bit tricky, you will need to create tar files of the subdirectories and move them and then untar the tar files in destination path. As this is a complex process, as a workaround, you can copy the files/directories recursively and then delete the files from original path.
cp -pr <source_path>/* <destination_path>/ && rm -rf <source_path>/*

Rsync make flat copy

I'm trying to write a script that copy all the files of one dir (with subdirs) to the root of another dir.
So Imagine I have this file structure:
/
pic.JPG
PIC5.JPG
FOLDER
pic2.JPG
pic3.JPG
FOLDER2
pic4.JPG
I want all the .JPG files from that directory and copy them over to another destination. But I don't want the directory structure, just the files.
This is what I've got:
"sudo rsync -aq --include '*/' --include '*.JPG' --exclude '*\' /source/picturesRoot/ /destination/flatView/
But it also copies the directories :(
I found this link on stackoverflow:
rsync : Recursively sync all files while ignoring the directory structure
I looked at the solution and didn't see much difference with my command, apart from the * and . in the path. I tried it but it didn't work.
I hope somebody can help me, thanks.
This answer cannot work for you because your pictures are not at the same level in directories. There is no option in rsync to skip the creation of directory structure. In the link you gave, it's working because the user explicitly select source files with *.
You can try something with find and rsync. Find will find files and rsync copy them.
Here is a solution :
find /source/picturesRoot -type f -name "*.JPG" -exec rsync -a {} /destination/flatView/ \;
Be careful, if two files have the same name just one will be in destination directory.

Extract all the files with name containing a keyword

I have thousands of html file in a directory. I wanna extract files that contain Chennai in the file name and put it into another folder. I am sure it is possible. I am not close enough to copy the files to another folder.
Use globbing:
mv *Chennai* target/
If the file names might start with a dot, use
mv .*Chennai* *Chennai* target/
Try:
find directory_with_htmls -type f -name "*Chennai*.html" -exec cp {} some_other_folder \;
This would copy html files in the directory_with_htmls directory containing Chennai in the name to the directory some_other_folder.

Script to extract zip files in seperate folders to their own folders

I have hundreds of folders each containing a zip file. I would like to extract each zip file to where they are located. Is there a simple trick or script to do this?
EDIT:
Each folder is under the same parent folder. So the hierarchy is as the following:
PARENT FOLDER
-SubFolder1
--somefile.zip
-Subfolder2
--somefile.zip
...
-SubfolderN
--somefile.zip
Under unix you could use something like
find <dir> -iname '*.zip' -execdir unzip {} \;
The program find traverses <dir> recursively and on every .zip file it finds it will change to that files directory and executes unzip on it.
Windows version:
for /r "C:\Some\Directory" %f in (*.zip) do unzip "%f" -d "%~dpf"
Warning: Completely untested.
References:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
http://www.info-zip.org/mans/unzip.html
I think with 7-zip it would be
for /r "C:\Some\Directory" %f in (*.zip) do 7z x -o "%~dpf" "%f"
but that's even untesteder.

Resources