1

I have a directory with many compiled C programs that I would like to back up. Right now they are named program.out but I would like to change this to just program.

Would it still be possible to exclude these? Also, what sort of regex would I use to quickly rename them all?

1
  • If you are just trying to rename some files, how is the script you posted relevant? I don't see any mention of program.out in the script anyways.
    – jw013
    Jul 10, 2014 at 0:53

3 Answers 3

0

For backing up your code use git or other version control software. On the internet you got lot of choices to name some: github, bitbucket.

In git you got file .gitignore if you add there *.out then it wont be transfered to the repository

2
  • I feel embarrassed putting all of my "baby's first C" code on the internet.
    – user341814
    Jul 10, 2014 at 16:52
  • Gitlab and bitbucket got private repos. On the other hand if you put it where public eye can see it your code will get better as you will tend to be better coder. Good luck! :)
    – 3h4x
    Jul 10, 2014 at 18:22
1

I'm not too clear on your first question. If you want to rename program.out to program then just run:

mv program{.out,}

For the second question, assuming I wanted to obtain a list of files in /etc with a suffix:

#!/bin/bash
ls /etc > /tmp/etcfiles
while read line
do
   if [[ $line =~ \..+ ]]; then
      echo $line
   fi
done < /tmp/etcfiles

You can adjust the above as required.

  • \. matches a literal period.
  • . matches any character following the period.
  • + says that at least one character must follow the initial period. So this will match test.c, but not test. If you want to match test. as well, then change the + to a *.
0

If you're using bash as a shell, this is a simple task for a shell pipeline.

ls | grep '\.out$' | while read file; do echo mv "$file" "${file%.*}"; done

ls produces a list of files in the directory.

grep '\.out$' extracts the filenames that end in .out and discards the rest.

while read file reads the filenames one at a time.

echo mv "$file" "${file%.*}" displays but does not run a mv command that would rename each file to a version without its extension. The ${file%.*} business is bash syntax for stripping off a file extension. When you're satisfied that the pipeline would do what you wanted if it were run for real, remove the echo part and let it run for real.

ls | grep '\.out$' | while read file; do mv "$file" "${file%.*}"; done
4
  • But how do I make a script ignore these files once they don't have the .out extension?
    – user341814
    Jul 10, 2014 at 1:02
  • Use the test command. test -x succeeds if a file's permissions have the execute bit set.
    – Kyle Jones
    Jul 10, 2014 at 1:07
  • That seems a bit hard... could you maybe help me with writing that? So I have t test if it is executable...and if it isn't, copy it.
    – user341814
    Jul 10, 2014 at 1:37
  • Can you please help me? I thought I ahd the gist of it, but now it just isn't working.
    – user341814
    Jul 10, 2014 at 1:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .