1

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?

1

3 Answers 3

1

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 {}'
Sign up to request clarification or add additional context in comments.

5 Comments

you don't need to copy the file name touch new one then remove the old, have a look to my answer
It looks cleaner in my opinion.
you can empty contents of a file by redirecting the output of /dev/null
That is another option. I find touch cleaner :)
This one fails if name contains brackets ()
0

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%%.*}

2 Comments

You can remove the cat /dev/null altogether as > file will empty it.
@MarkSetchell great, then I will update the answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.