In "sftp" how to upload all files, but not (sub)folders - linux

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/*

Related

How to rsync all folders present on the destination folder

I have two folders FolderA and FolderB as below. I want to rsync the common subfolders. For example, I can do rsync -avzP /path/to/FolderB/* /path/to/FolderA/, which will keep SubFolder1 and SubFolder3 mirrored. My question is how I can achieve the same if FolderB is the destination without explicitly --include or --exclude individual subfolders (e.g., in case there are too many of them).
FolderA
|--SubFolder1
|--SubFolder2
|--SubFolder3
|--SubFolder4
FolderB
|--SubFolder1
|--SubFolder3
You can update all files in FolderA using the contents FolderB as the source with your normal:
rsync -uav /path/to/FolderB/ /path/to/FolderA
(note: the trailing '/' after FolderB/ is mandatory to copy the contents of FolderB rather than FolderB itself)
To do it in reverse and update FolderB from FolderA and not copy SubFolder2 and SubFolder4 with the --existing option which will "skip creating new files on receiver", but that will also prevent new files and directories within SubFolder1 and SubFolder3 from being created as well.
You best option to not copy SubFolder2 and SubFolder4 while allowing new files and directories within SubFolder1 and SubFolder3 be created is to use the --filter option. See rsync(1) - Linux manual page.
A typical way to use --filter to exclude SubFolder2 and SubFolder4 on a copy from FolderA to FolderB would be:
rsync -uav --filter -_SubFolder2/ --filter -_SubFolder4/ /path/to/FolderA/ /path/to/FolderB
That will allow you to copy the complete contents of /path/to/FolderA/ to /path/to/FolderB/ without including SubFolder2 and SubFolder4.
Edit Per-Comment On Large Number of SubFolders
If you have a large number of folders under FolderA that you do not want to sync under FolderB, then your other option is to create a text file holding the absolute path to only those SubFolderX under FolderA you want to rsync to FolderB and then use the --no-R and --files-from=folderlist options to only rsync the wanted SubFolders. This will eliminate having to specify a large number of --filter options on the command line.
For example, you can create your folderlist with:
find /path/to/FolderA -maxdepth 1 -type d > folderlist
(note: specify the absolute path above and find will produce the folderlist file containing absolute paths)
Now edit your folderlist file and remove the parent directory (e.g. /path/to/FolderA) and any SubFolders you don't want to sync under FolderB. You can now use the folderlist file to control which SubFolders under FolderA are sync'ed to FolderB without having to include a long list of filters on the command line. Your command line then becomes
rsync -uai -r --no-R --files-from=folderlist / /path/to/FolderB
(note: the '/' as source serves as a base for the paths contained in folderlist. You can change the -i option to control the level of information dumped to the screen, e.g. -v, etc... or remove it altogether to suppress any reporting other than errors)
(also note: when using --files-from, -a does not imply -r (recursive), so you will need to explicitly add -r if you need a recursive transfer)

unzip in current directory while preserving file structure

I'm in a directory and I have a zip containing files and directories.
I need to unzip that file, into current directory, but preserving the file structure.
unzip myfile.zip will create a myfile directory in current directory which is not what I want.
unzip -j myfile.zip will kill all the file strucure, which is not what I want.
unzip myfile.zip extracts files in the working directory by keeping path names from the zip file.
So if you get a subdirectory myfile it means it is part of the relative path of compressed files. Check it by listing the zip content
unzip -l myfile.zip
So you can unzip the file from the directory above, or, from the target directory unzip with -d option, where -d is the directory above
cd myfile
unzip myfile.zip -d ..
Dont select the folder while zipping.
For example
myfile/abc.txt and myfile/efg.txt
so while zipping select the files (abc.txt,efg.txt) and zip dont select the myfile folder to zip.
So that when you unzip the file, the parent dir for each file or folder will be the directory in which you unzip.
The myfile directory was zipped into the zip file when it was created and looking at the unzip options there isn't a way to do this without adding additional steps.
If this entire process is under your control you should look at either creating the zip without using including the parent directory or you could use an alternative like tar (to create and extract) which allows you to extract content from the repo with greater precision.

Shell: How to move multiple files to a directory and zip that directory with different name?

Fore example, I have some files in my project, structure could be like this
Project/
-- file1.txt
-- file2.txt
-- build/
-- sub-folder/
I want to zip some files from my project and I can do that using ZIP command (build folder and some other files are excluded from zip)
zip -r build/project-04.05.2016 ./ -x *\build\*
After this new file is created:
build/project-04.05.2016.zip
In Mac Finder when I double click on this file it becomes unzipped like this:
build/project-04.05.2016/
----------- file1.txt
----------- file2.txt
----------- subfolder/
I would like to somehow zip this archive, so when it's unzipped, instead of "project-04.05.2016" I get a folder "project" with the same content. I was trying to rename the file to "project-04.05.2016" after it's zipped as "project", but when it's unzipped the results are the same. Maybe there's a way to first move the files to some temporary "project" folder and than to zip them to "project-04.05.2016.zip"? Thanks in advance.
Here is my current solution, it's a small script and I'm not too satisfied with it, but it works. I will accept if someone answers with more elegant solution. So, here it is:
#clear the contents of the previous build first
rm -r build/*
#copy required files to a temporary folder
rsync -av --exclude=build ./ build/project
#change current directory to build
cd build/
#zip files from build/project folder
zip -r "project-04.05.2016" project
#remove temporary folder
rm -r project/
#final zip file is at location:
#build/project-04.05.2016.zip

recursively move, rename and zip files

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

add files to a specafic directory inside zip file

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.

Resources