Queen Queen - 11 months ago 93
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

test.txt
. The file is located in
/Users/xx/xx
.
The problem is that, the name of the file is different every time (For example:
part-r-00000-865affea-3ead-445e-ad3e-8703a8d79026.csv
) 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
part-r-00000-865affea-3ead-445e-ad3e-xxxx.csv
(the name always change) into
test.txt


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