I have a folder structure like this
Folder1 Folder2 Folder3 ...FolderXYZ
Each of these Folders contains around 500 files named like
event-yyyy-mm-dd-0001.jpg_backup event-yyyy-mm-dd-0002.jpg_backup ... and
event-yyyy-mm-dd-0001.jpg event-yyyy-mm-dd-0002.jpg ....
I need to copy all *.jpg_backup files from Folder1 Folder2 ... to a new created subdirectory within a diffrent folder Folderexisting, strip the _backup from the filenames, and make a zip with all the renamed files, named after parent directory (e.g. Folder1.zip).
The script should be able to do this recursively for all folders. OS is a Debian Wheezy with no GUI.
THX in advance
Related
In an sftp session, is there a way for me to put the contents of a folder, but only files (not subfolders)? Here's an example.
Folder
main.py
config.py
requirements.txt
__pycache__ (a folder)
Above is a sample of the local directory. From the folder that encloses Folder, I'd SFTP to the target server. How do I put only main.py, config.py, and requirements.txt (they're files not folders)? . I don't want to put __pycache__ as it's a folder.
If I put -r Folder it will copy Folder and all its contents, including __pycache__. If I put -r Folder/*, it will put all the contents of Folder, without Folder itself, including __pycache__. This is close to what I want. A variant of put -r Folder/* that only copies file contents, not subfolders. So it would skip the __pycache__ folder when copying contents.
Thanks!
Just remove the -r if you do not want to recurse into the subdirectories:
put Folder/*
for file in os.listdir(r'C:\Users\user\Downloads\plant_tomato_leaf_dataset\plantvillage'):
for cl in os.listdir(r'C:\Users\user\Downloads\plant_tomato_leaf_dataset\plantvillage\\'+file):
print(file)
print(cl)
print('******************')
I have a folder "plantvillage". Inside that folder I have 10 folder. I have to access all the sub-folders and perform operations on those sub-folder-items. But this code is showing only items of the first folder
You might be interested in os.walk instead of os.listdir. listdir is not recursive.
An example would be:
for root, dirs, files in os.walk(r'C:\Users\user\Downloads\plant_tomato_leaf_dataset\plantvillage'):
for file in files:
print(os.path.join(root,file))
this will run the inner loop as many times as there are directories, and will also check to make sure that each file is actually a file. If you want to look at the directories themselves for some reason (ignoring files), you can do that like so:
for root, dirs, files in os.walk(r'C:\Users\user\Downloads\plant_tomato_leaf_dataset\plantvillage'):
for directory in dirs:
print(os.path.join(root,directory))
but don't try to mix and match. Getting the current directory is easy when iterating files. It's just root.
On Linux, I am trying to create a .tar.gz archive from a different directory, that is I have a bash script that will be executed from a different directory. The script will package the folder, I will give the absolute directory of the folder say /home/user1/Documents/folder1 however when it packages the tar file, it puts the entire absolute directory in the archive, whereas I only want the relative one from folder1.
For example:
tar czf /home/user1/Documents/folder1.tar.gz /home/user1/Documents/folder1
This will create an archive but where the first folder will be home and then inside that user1 inside that documents and inside that the folder1, no other subfolders from other branches of course.
Also the console gives this error:
tar: Removing leading `/' from member names
I want it to be packaged as if I would execute the command from the same folder, so the only folder in the archive should be folder1, and inside that it's own subfolders.
So the archive inside should look just as if I would have executed this code from the same directory folder1 is in:
tar czf folder1.tar.gz folder1
You can use the -C option to change the directory before performing any operations:
tar czf /home/user1/Documents/folder1.tar.gz -C /home/user1/Documents folder1
Now, the contents of your archive will look like this:
$ tar tf /home/user1/Documents/folder1.tar.gz
folder1/
folder1/file1
The message you get is not an error, by the way. It's tar telling you that it made the paths in the archive relative so as to avoid overwriting files, which could easily happen when unpacking an archive with absolute paths. You can turn off the leading slash removal with -P, but you often don't want that.
Using a shell script I wish to delete all files and folders from /folder2/ that do not exist in /folder1/. Files only need to be matched by name.
I must add that the content of both folders shouldn't necessarily match after this operation because it's possible that /folder1/ contains files that do not in exist in /folder2/. So after executing the shell script all files and folders found in /folder2/ can also be found in /folder1/ but not vice versa.
The following works for me:
rsync -r --delete --existing --ignore-existing /path/to/folder1/ /path/to/folder2/
rsync will delete all files and folders from folder2 that are not found in folder1 recursively. Also, rsync will skip creating files on the destination. This answer was found here: https://serverfault.com/a/713577
Let's say we have a zip file contains a directory named aq and in the current working directory we have files:
./
|- aq/a.txt
|- b.txt
When i use this command:
zip test.zip aq/* the a.txt file will be zipped into the aq directory that's inside the zip file
The question is how then can I add b.txt into the aq directory that's inside the test.zip file without putting the b.txt in the aq directory first which is in the current working directory like what I did with the a.txt?
Make a temp directory called, e.g. /tmp/$$/aq/, symlink into the temp directory and then do:
(cd /tmp/$$ && zip -r $ZIPDEST aq/)
i.e. zip using the temp dir. zip by default follows symbolic links, so it puts the file into the zip without making a copy.
This is pretty much how I construct complicated hierarchical zip files without copying everything to make the archive.
Tar has better options for renaming items as you're putting them into the archive, but you asked about zip.