How to unzip recursively in place? - zip

I have a directory with subdirectories. I want to unzip every .zip file in this directory structure recursively and locally. (Unzip next to the .zip file)
For example:
.
├── A
│   ├── A2
│   │   └── content1.zip
│   ├── content2.zip
│   └── content3.zip
├── B
│   └── content4.zip
└── content5.zip
After unzip:
.
├── A
│   ├── A2
│   │   ├── content1.txt
│   │   └── content1.zip
│   ├── content2.txt
│   ├── content2.zip
│   ├── content3.txt
│   └── content3.zip
├── B
│   ├── content4.txt
│   └── content4.zip
├── content5.txt
└── content5.zip
I prefer if it's working both on linux and windows.

With python3
import os
import sys
import zipfile
for dir in os.walk(sys.argv[1]):
#dir[0] - current directory
#dir[1] - directories in current directory
#dir[2] - files in current directory
for file in dir[2]:
if file.endswith(".zip"):
with zipfile.ZipFile(dir[0] + "/" + file, 'r') as zip_ref:
zip_ref.extractall(dir[0])
Run in current directory:
python3 unzip.py .

Related

Archive & delete files older than x days, maintaining the directory structure

we have log files generating from different sources and storing in directories & sub-directories. The directory structure is something like below.
I want to tar & zip the file older than 30 days maintaining the directory structure and delete the archived files after the tar & zip.
Can someone help me with this? How this can be achieved.
dataload
├── apiConnectorApp
│   ├── csv
│   │   ├── 20210216231308
│   │   ├── batch1
│   │   ├── batch2
│   │   └── batch3
│   ├── day1Load
│   ├── logs
│   └── sql
├── configs
│   ├── eSite
│   │   ├── CKB2B
│   │   │   ├── CatalogEntryAssociations
│   │   │   ├── CatalogGroup
│   │   │   └── CatalogGroupCatalogEntryRelation
│   │   └── THB2B
│   │   ├── CatalogEntryAssociations
│   │   ├── CatalogGroup
│   │   └── CatalogGroupCatalogEntryRelation
find dataload -type f -mtime +30 -exec tar -rf archive.tar {} + -exec rm -f {} +
Run find with dataload as the parent directory. Process files that have been modified more that 30 days ago. Append as many files as possible (+) to an archive file "archive.tar" by using the tar -r flag. Then remove the files.

Python3.5 recursive glob not showing matching files

With Pyton >= 3.5 glob supports recursive searching. I am attempting to do this, but with no success.
Here you can see my directory structure:
/Volumes/user/TEST/
└── [NAME]john\ doe,\ peter\ griffin\ -\ sample\ video[29.06.17][1080]{user}
├── [NAME]john\ doe,\ perter\ griffin\ -\ sample\ video[29.06.17][1080]{user}.mp4
├── [NAME]john\ doe,\ peter\ griffin\ -\ sample\ video[29.06.17][Hi-Res]
│   ├── 583a6ea574035-img-8399.jpg
│   ├── 583a6eb9681c0-img-8401.jpg
│   ├── 583a6eb9e53e1-img-8402.jpg
│   ├── 583a6ecad078d-img-8404.jpg
│   ├── 583a6ecd207ed-img-8403.jpg
│   ├── 583a6edb7348c-img-8405.jpg
│   ├── 583a6ede1cbfa-img-8406.jpg
│   ├── 583a6eeb294a9-img-8407.jpg
│   ├── 583a6eee94a45-img-8408.jpg
│   ├── 583a6efb2389a-img-8409.jpg
│   ├── 583a6f01a78ae-img-8410.jpg
│   ├── 583a6f0daea30-img-8411.jpg
│   ├── 583a6f1301e3c-img-8412.jpg
│   ├── 583a6f1ec07d8-img-8413.jpg
│   ├── 583a6f240d4ce-img-8414.jpg
│   ├── 583a6f306f517-img-8415.jpg
│   ├── 583a6f36def7a-img-8416.jpg
│   ├── 583a6f4280706-img-8417.jpg
│   ├── 583a6f48b8b11-img-8418.jpg
│   ├── 583a6f55160cc-img-8419.jpg
│   ├── 583a6f5b7fb35-img-8420.jpg
│   ├── 583a6f68aad40-img-8423.jpg
│   ├── 583a6f6da8c0b-img-8424.jpg
│   ├── 583a6f7b9e1d6-img-8425.jpg
│   ├── 583a6f8069e59-img-8426.jpg
│   ├── 583a6f8e9891e-img-8427.jpg
│   ├── 583a6f9427910-img-8428.jpg
│   ├── 583a6fa45e3f4-img-8429.jpg
│   ├── 583a6fa96b182-img-8430.jpg
│   ├── 583a6fb935bf2-img-8431.jpg
│   ├── 583a6fca7a0c9-img-8433.jpg
│   ├── 583a6fcbab549-img-8434.jpg
│   ├── 583a6fdcf2812-img-8435.jpg
│   ├── 583a6fde5ee86-img-8436.jpg
│   ├── 583a6fee54d9b-img-8437.jpg
│   ├── 583a6ff0acb76-img-8438.jpg
│   ├── 583a7000ecd80-img-8439.jpg
│   ├── 583a70026c325-img-8440.jpg
│   ├── 583a701319c22-img-8441.jpg
│   ├── 583a701457221-img-8443.jpg
│   ├── 583a7027e8531-img-8444.jpg
│   ├── 583a7029f0e7c-img-8445.jpg
│   ├── 583a703d6c3c9-img-8446.jpg
│   ├── 583a703f879f5-img-8447.jpg
│   ├── 583a7051ae77a-img-8448.jpg
│   ├── 583a705b588c3-img-8449.jpg
│   ├── 583a706d40811-img-8450.jpg
│   ├── 583a7076cb3e9-img-8451.jpg
│   ├── 583a70894ec35-img-8452.jpg
│   ├── 583a70964fbcb-img-8453.jpg
│   ├── cai-8498-pa.jpg
│   └── cai-8498-pt.jpg
├── [NAME]john\ doe,\ peter\ griffin\ -\ sample\ video[29.06.17][Hi-Res].jpg
└── cover.png
So the images are definitely there.
Now here is my method, that for some reason, is not working:
def count_images(folder):
"""
Count the number of images in a folder.
:param folder: Folder to count the images
:return: Number of photos
"""
print(folder)
files = [file for file in glob.glob(folder + '/**/*.jpg', recursive=True)]
print(files)
return len([file for file in glob.glob(folder + '/**/*.jpg', recursive=True)])
But it just outputs the following:
/Volumes/user/TEST/[NAME]john doe, peter griffin - sample video[29.06.17][1080]{user}/
[]
It is becoming extremely frustrating, as I do not see what I am doing wrong.
So I found the problem.
The glob module has issues with certain characters in the filename - [ and ] in this case. You can use glob.escape() to escape these characters in the filename.
So the final function will look like this:
def count_images(folder):
return len([file for file in glob.glob(glob.escape(folder) + '/**/*.jpg', recursive=True)])

Installing Viki on Vim 7.4.52

I'm following Swaroop's Byte of Vim, and have reached the chapter on personal information management, where it says to install the Viki plugin. The instructions are as follows (and I have no real idea of what is going on, but):
Download multvals.vim [2] and store as $vimfiles/plugin/multvals.vim
Download genutils.zip [3] and unzip this file to $vimfiles
Download Viki.zip [4] and unzip this file to $vimfiles (make sure all the folders and files under the 'Viki' folder name are stored directly in the $vimfiles folder)
After this I open a new text file in vim and run the commmand
:set filetype=viki
but I get a whole slew of errors. I've tried clearing out my ~/.vim folder and reinstalling everything, along with tlib this time as specified on the Viki vimscript page, and extracted the version 4.0 viki.vba instead of using the version 4.08 zip file, but I'm still getting errors about non-existent functions:
Error detected while processing home/user/.vim/ftplugin/viki.vim:
line 100
E117: Unknown function: tlib#balloon#Register
I don't really know what's going on, and am quite a new Vim user, so please be patient. Right now my ~/.vim directory tree looks like this:
.vim
├── autoload
│   ├── genutils.vim
│   ├── tlib
│   │   ├── eval.vim
│   │   ├── list.vim
│   │   ├── notify.vim
│   │   ├── persistent.vim
│   │   ├── progressbar.vim
│   │   ├── TestChild.vim
│   │   └── vim.vim
│   ├── tlib.vim
│   ├── viki
│   │   ├── enc_latin1.vim
│   │   └── enc_utf-8.vim
│   ├── viki_anyword.vim
│   ├── viki_latex.vim
│   ├── viki_viki.vim
│   └── viki.vim
├── colors
│   └── molokai.vim
├── compiler
│   └── deplate.vim
├── doc
│   ├── tlib.txt
│   └── viki.txt
├── ftplugin
│   ├── bib
│   │   └── viki.vim
│   └── viki.vim
├── plugin
│   ├── 02tlib.vim
│   ├── genutils.vim
│   ├── multvals.vim
│   └── viki.vim
└── test
└── tlib.vim
Any help is much appreciated.
The info is outdated. You need to install a current versions of tlib and viki from:
https://github.com/tomtom/viki_vim
https://github.com/tomtom/tlib_vim

ctrlp still searches the ignored directory

I tried to put ignored setting in .vimrc
But when I used the ctrlp to search under rails app folder
It still search the vendor folder, so it took lots of time.
But when the search was done, I couldn't search anything under the vendor
It was so strange! How to fix it.
Here is my .vimrc setting file.
http://d.pr/i/yMtK
http://d.pr/i/Hy4u
" Sane Ignore For ctrlp
let g:ctrlp_custom_ignore = {
\ 'dir': '\.git$|vendor\|\.hg$\|\.svn$\|\.yardoc\|public\/images\|public\/system\|data\|log\|tmp$',
\ 'file': '\.exe$\|\.so$\|\.dat$'
\ }
When I appended the code in the end of .vimrc
217 let g:NERDTreeIgnore=['\~$', 'vendor']
218 set wildignore+=*\\vendor\\**
It worked when my first time to use the CTRLP to search under the RAILS app folder,
But still NOT worked in the following times.
I guess maybe there are some settings will disable the ignored setting ?
Here are the structure of my folder
.
├── Gemfile
├── Gemfile.lock
├── README.rdoc
├── Rakefile
├── app
│   ├── assets
│   ├── controllers
│   ├── helpers
│   ├── mailers
│   ├── models
│   ├── uploaders
│   ├── views
│   └── workers
├── auto.sh
├── config
│   ├── application.rb
│   ├── application.yml
│   ├── boot.rb
│   ├── database.yml
│   ├── environment.rb
│   ├── environments
│   ├── initializers
│   ├── locales
│   ├── macbookair_whenever_schedule.rb
│   ├── menu_navigation.rb
│   ├── navigation.rb
│   ├── resque.god
│   ├── resque_schedule.yml
│   ├── routes.rb
│   ├── schedule.rb -> ubuntu_whenever_schedule.rb
│   ├── tinymce.yml
│   └── ubuntu_whenever_schedule.rb
├── config.ru
├── db
│   ├── development.sqlite3
│   ├── migrate
│   ├── migrate_should_be_skip
│   ├── production.sqlite3
│   ├── schema.rb
│   └── seeds.rb
├── doc
│   └── README_FOR_APP
├── lib
│   ├── assets
│   ├── auto_tools
│   ├── tasks
│   └── url_automation_module.rb
├── log
│   ├── apalog
│   ├── development.log
│   ├── passenger.80.log
│   ├── production.log
│   └── prodution.log
├── output_name
├── public
│   ├── 404.html
│   ├── 422.html
│   ├── 500.html
│   ├── exports
│   ├── favicon.ico
│   ├── results.zip
│   ├── robots.txt
│   ├── sandbox
│   └── uploads
├── script
│   ├── delayed_job
│   └── rails
├── test
│   ├── fixtures
│   ├── functional
│   ├── integration
│   ├── performance
│   ├── test_helper.rb
│   └── unit
├── test.sh
├── tmp
│   ├── cache
│   ├── pids
│   ├── restart.txt
│   ├── sessions
│   └── sockets
├── tmplog
└── vendor
└── bundle
If you type :help ctrlp-options and read a bit, you will find:
Note #1: by default, wildignore and g:ctrlp_custom_ignore only
apply when globpath() is used to scan for files, thus these options
do not apply when a command defined with g:ctrlp_user_command is
being used.
Thus, you may need to unlet g:ctrlp_user_command (possibly set to a default command) to actually use wildignore as advised by #TomCammann. For instance, in your ~/.vimrc, add:
if exists("g:ctrlp_user_command")
unlet g:ctrlp_user_command
endif
set wildignore+=*\\vendor\\**
After that, you need to refresh your ctrlp cache: in Vim, press F5 in ctrlp mode, or run :CtrlPClearAllCaches, or remove the cache directory directly in your shell:
rm -r ~/.cache/ctrlp/ # On Linux
part of my .vimrc file . perhaps it will help
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.idea/*,*/.DS_Store,*/vendor
You can use the wildignore vim setting which CtrlP will pick up on.
set wildignore+=*\\vendor\\**
Check if you are using some specific search command, like:
let g:ctrlp_user_command = 'find %s -type f' " MacOSX/Linux
let g:ctrlp_user_command = 'dir %s /-n /b /s /a-d' " Windows
This kind of configuration ignores the g:ctrlp_custom_ignore option.
wildignore may be used by other commands, the reason of failure for g:ctrlp_custom_ignore is g:ctrlp_user_command, for example, here is mine:
if executable('rg')
let g:ctrlp_user_command = 'rg %s --files --hidden --color=never --glob ""'
endif
For this case, rg has own ignore way, just put .git to .gitignore, rg will not search any files in .gitignore.

Copy every file of entire directory structure into base path of another

I have a directory-tree with a lot of files in it. I'd like to copy all of those files into one new directory, but with all files located in the base of the folder.
So I have something like this:
images
├── avatar.png
├── bg.jpg
├── checkbox.png
├── cross.png
├── datatables
│   ├── back_disabled.png
│   ├── back_enabled.png
│   ├── forward_disabled.png
│   ├── forward_enabled.png
│   ├── sort_asc.png
│   ├── sort_asc_disabled.png
│   ├── sort_both.png
│   ├── sort_desc.png
│   └── sort_desc_disabled.png
├── exclamation.png
├── forms
│   ├── btn_left.gif
│   ├── btn_right.gif
│   ├── checkbox.gif
│   ├── input
│   │   ├── input_left-focus.gif
│   │   ├── input_left-hover.gif
│   │   ├── input_left.gif
│   │   ├── input_right-focus.gif
│   │   ├── input_right-hover.gif
│   │   ├── input_right.gif
│   │   ├── input_text_left.gif
│   │   └── input_text_right.gif
│   ├── radio.gif
│   ├── select_left.gif
│   ├── select_right.gif
And I'd like something like this:
new_folder
├── avatar.png
├── bg.jpg
├── checkbox.png
├── cross.png
├── back_disabled.png
├── back_enabled.png
├── forward_disabled.png
├── forward_enabled.png
├── sort_asc.png
├── sort_asc_disabled.png
├── sort_both.png
├── sort_desc.png
├── sort_desc_disabled.png
├── exclamation.png
├── btn_left.gif
├── btn_right.gif
├── checkbox.gif
├── input_left-focus.gif
├── input_left-hover.gif
├── input_left.gif
├── input_right-focus.gif
├── input_right-hover.gif
├── input_right.gif
├── input_text_left.gif
├── input_text_right.gif
├── radio.gif
├── select_left.gif
├── select_right.gif
I'm pretty sure there is a bashcommand for that, but I haven't found it yet. Do you have any ideas?
CS
find /source-tree -type f -exec cp {} /target-dir \;
you are looking for ways to flatten the directory
find /images -iname '*.jpg' -exec cp --target-directory /newfolder/ {} \;
find all files iname in case insensitive name mode.
cp copy once to --target-directory named /newfolder/.
{} expand the list from find into the form of /dir/file.jpg /dir/dir2/bla.jpg.
On zsh:
cp /source/**/* /destination
$ cd images && cp ** new_folder/

Resources