Compress multiple files individually with 7zip - shell

Searching I noticed that it was possible to compress several files individually by gzip. Unfortunately this format is not useful to me.
I tried to compose with the following command.
Find. -type f -name "* .txt" -exec 7z {} \;
Suggestions?

To add a file to archive, you should use "a" command: 7z a archive.7z file. So your command should be like this:
find . -type f -name "*.txt" -exec 7z a $(basename {}).7z {} \;
or, if you want archive names like ".txt.7z",
find . -type f -name "*.txt" -exec 7z a {}.7z {} \;
basename here is to get your file name without extension.

Related

Copy all *.txt to *.new.txt but ignore existing *.new.txt files

I need script for this:
Copy all *.txt to *.new.txt
When run this script, ignore all *.new.txt files.
What I try:
find ~/folder -type f -name "*.txt" ! -name "*.new.txt"
how to send this output to
cp STDIN *.new.txt
Use -exec to execute a subshell where you copy the file to the modified filename.
find ~/folder -type f -name "*.txt" ! -name "*.new.txt" -exec bash -c 'cp "$1" "${1/.txt/.new.txt}"' {} {} \;

Mac terminal how can i search in all folders and subfolders for image

How can i search for example all .png files on an external disk and copy them to another directory?
Have tried to use the cp command. Have try it but don't work for me
?
Monterey 2.2.1
cp /Volumes/Data *.png /Volumes/Data/pictures_png
cp command won't work if you need to recursively copy from the sub directories. You need to use find.
Syntax:
find $SOURCE -type f -name '*.type' -exec cp '{}' $DESTINATION ';'
In your case,
find /Volumes/Data -type f -name '*.png' -exec cp '{}' /Volumes/Data/pictures_png ';'
Here is how it works:
-type f means copy only files not directories.
-name is to provide the filename to find. Here *.png for pattern matching
-exec executes the following line for each result the above find returns.
{} will be replaced with the results from find
; terminates -exec command

How to extract files with extension from directories?

I am currently extracting pictures from a large multi-levels directory using the following bash command:
find . -name \*.jpg -exec cp {} /newdir_path_.. \;
However, all pictures are stored under 3 versions:
xxx-LD.jpg
xxx-SD.jpg
xxx.jpg
I just want to extract the xxx.jpg pictures, not the LD and SD...
how should my command be modified to perform such extraction?
You can add more tests:
find . -name '*.jpg' -not -name '*-[LS]D.jpg' -exec cp {} /newdir_path_.. \;
-not is a GNU extension; you can use ! -name instead. In some shells, ! has to be escaped: \! -name.
This may also work for you considering your filenames only contain digits before .jpg:
find . -iregex '.*/[0-9]*\.jpg$' -exec cp {} /newdir_path_.. \;

How to print the deleted file names along with path in shell script

I am deleting the files in all the directories and subdirectories using the command below:
find . -type f -name "*.txt" -exec rm -f {} \;
But I want to know which are the files deleted along with their paths. How can I do this?
Simply add a -print argument to your find.
$ find . -type f -name "*.txt" -print -exec rm -f {} \;
As noted by #JonathanRoss below, you can achieve an equivalent result with the -v option to rm.
It's not the scope of your question, but more generally it gets more interesting if you want to delete directories recursively. Then:
a simple -exec rm -r argument keeps it silent
a -print -exec rm -r argument reports the toplevel directories you're operating on
a -exec rm -rv argument reports all you're removing

Cant find .gz file in AIX

I basically have written a shell script in AIX that will delete some old log file and will compress some .
This is my script
#!/bin/sh
###
### Static variables
###
nmon_dir="/var/log/applog/nmon"
cd $nmon_dir
find $nmon_dir -xdev -type f -mtime +360 -name "*.nmon*" -exec rm {} \;
find $nmon_dir -xdev -type f -mtime +300 -name "*.nmon" -exec gzip {} \;
I could delete the files as i wanted but that I am not sure whether it compressed those file . Because i couldn't find .gz file both in root or /var/log/applog/nmon path .
Need Help!
Seems to me like your KSH might be taking the {} you pass to find as a compound command definition instead of as the filename placeholder. Try escaping it, I use it as \{\} and it never gives me problems.

Resources