Queen Queen - 2 years ago 185
Bash Question

Change the name of file in directory with csv extention

I would like to change a file name with extension of csv into

. The file is located in
The problem is that, the name of the file is different every time (For example:
) and it is not predictable. However, the extension is always csv and the location also is constant.

I should mention that, the only file that exist in the folder each time is the mentioned csv file so, there is only one file in the folder that each time its name becomes different.

In fact, I need to change
(the name always change) into

Can you help me?

Answer Source

You can use globbing to target a file without knowing its whole name.

Considering you know the file's directory, its extension, and that it is the only one with this extension in this directory, you can simply use this mv :

mv /Users/xx/xx/*.csv /Users/xx/xx/test.txt

This will rename every .csv file in /Users/xx/xx/ (in your case, your single CSV file) into /Users/xx/xx/test.txt.

That would obviously be a problem if there was multiple .csv files since each would overwrite the previous one. If it could become a problem, you should either check the number of files which are matched by this glob and exit with error if it's greater than 1 or use a command that will only ever act on a single file such as the one suggested by RomanPerekhrest.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download