Executes your command for each file selected using glob expression(s).
Project description
para-cada
Para Cada in Spanish means For Each. The tool executes your command for each file selected using glob expression(s).
Why? Let's say you have multiple .tgz
archives and you would like to extract them in one shot. Some of the options available in bash are:
ls *.tgz | xargs -IT tar xzvf T
for T in *.tgz; do tar xzvf $T; done
find . -type f -name '*.tgz' -exec tar xzvf {} \;
All of them are relatively complex. This is where cada can help. Simply do:
cada 'tar xzvf *.tgz'
Cada knows where glob expression is. It executes entire command with subsequent values corresponding to this expression. Additionally, user may transform those values using regular Python syntax. Take a look at the examples below and the tutorial.
Installation
pip install para-cada
Examples
It is recommended to run examples below in the dry mode, by adding -d
flag. This way you will only simulate what would happen without actually applying any changes to the filesystem.
Examples below assume that there are not spaces or special characters in the filenames. Otherwise, some quotation would be required.
# backup all the `.txt` files in the current directory
cada 'cp *.txt {}.bkp'
# restore backups above
cada 'mv *.bkp {p.stem}'
# replace `conf` and `config` by `cfg` in the file names of `.ini` files; be case insensitive
cada 'mv *.ini {}' 're.sub("conf(ig)?", "cfg", s, flags=re.IGNORECASE)'
# change file names from snake-case to camel-case, leave extensions in lower case
cada 'mv *.* {}' 'Path(s.title().replace("_", "")).with_suffix(p0.suffix.lower())'
# prepend each `.txt` file with subsequent numbers; 4 digits wide, 0-padded
cada 'mv *.txt {i:04d}_{}'
# add `.d` suffix to the names of all directories
cada 'mv * {}.d' -f x.is_dir
# print filenames where stem is shorter than 3 characters
cada 'echo *' -f 'len(p.stem) < 5' -s
# to each `.tar` file add a suffix that represents MD5 sum calculated over the file content
cada 'mv *.tar {s}.{e}' 'hashlib.md5(p.read_bytes()).hexdigest()' -i hashlib
# set executable attribute to the files with a shebang and remove it from remaining files
cada 'chmod {}x **/*' '"-+"[p.open("rb").read(2) == b"#!"]' -f x.is_file
# put your images in subdirectories according to their creation date
cada 'mkdir -p {} && mv *.jpg {}' x.ctime
# put your images in subdirectories according to their MIME type
cada 'mkdir -p {} && mv * {}' 'sh("file {s} -b --mime-type")'
# compile simple C++ project without any build system
mkdir -p build
cada 'g++ -c src/*.cpp -I inc -o build/{p.stem}.o'
g++ build/*.o -o build/app
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for para_cada-1.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 461360b23b3e09f9a84f780c7a7807314f973e81c28bb7eae581151f75eeedeb |
|
MD5 | 02d1a588a78769e0521e826c9bf70ffe |
|
BLAKE2b-256 | 9dadcf7ed784238029a98d49907aec2ccc57ae515eb2740852e0da7760a7c16f |