Folder contains some files:
file.exe, example.exe etc.
The task is to create empty files in the same folder with matching names but different extension:
file.cfg, example.cfg
then delete all .exe files.
How do I do that?
Folder contains some files:
file.exe, example.exe etc.
The task is to create empty files in the same folder with matching names but different extension:
file.cfg, example.cfg
then delete all .exe files.
How do I do that?
Find all files with .exe, replace file ending. touch creates files. rm removes files. Inspiration taken from here.
find *.exe -print -type f | xargs -I {} bash -c 'filename={}; touch ${filename%.exe}.cfg; rm {}'
/dev/nulltouch cleaner :)Additionally, you can empty contents of a file by redirecting output of /dev/null to it (file) as input using cat command:
cat /dev/null > file.exe
alternative as per Mark Setchell suggestion You can remove the cat /dev/null altogether as > file will empty it :
> file.exe
just to make it easy and clean you have to loop over these files and do the needful for each one of them as below:
cd /yourabsolutepath/
for i in *.exe
do
> "$i" && mv "$i" "${i%%.*}.cfg"
done
to remove suffix .exe you can use Substitution ${i%%.*}
cat /dev/null altogether as > file will empty it.You can do it pretty succinctly with GNU Parallel:
parallel '> {.}.cfg ; rm {}' ::: *.exe
If you want to do a dry-run to see what it would do without actually doing anything:
parallel --dry-run '> {.}.cfg ; rm {}' ::: *.exe
Sample Output
> a.cfg ; rm a.exe
> b.cfg ; rm b.exe
> c.cfg ; rm c.exe