How to remove `_` from all file-names in a directory

obidyne

In bash, I have an unzipped directory where I have many file names that start with _.

I'm pretty sure I can use rename or mv to simply remove the _ prefix of each file that has it.

I try:

for file; do
    mv "$file" "${file//_/}"
done

But that doesn't have the effect I'd like. What command can I use to remove the _ prefix from all files?

ilkkachu

1) for file; do loops over the positional parameters, i.e. command line arguments to the script. Use for file in *; do to loop over the files in the directory (or _* to just take the ones with an underscore prefix.)

2) You can use ${file#_} instead of ${file//_/} to remove the underscore from the beginning of the filename. ${par#word} specifically removes a part from the beginning, and it's a standard feature, unlike ${par/pat/repl}. And of course ${file//_} would remove all slashes (since you used a double-slash), not just the first one.

3) at least on Linux (GNU userland) and FreeBSD, you can use mv -n to ask it to not overwrite any files. Just in case.

So,

for file in _*; do
    mv -n -- "$file" "${file#_}"
done

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove files from a directory if their names are not in a text file? Bash script

From Dev

remove all leading and trailing whitespace from both file and directory names recursively

From Dev

remove date from file names for many files in a directory

From Dev

String from file names - How remove the extension?

From Dev

How to remove URI encoding from file names?

From Dev

How to remove a number from file names?

From Dev

How to remove all files from a directory?

From Dev

Subtract 1 from all file names (rename them) in a directory.

From Dev

Batch script to remove parentheses and spaces from all file names

From Dev

How to remove a directory path from a text file

From Dev

How to remove single file from /usr directory?

From Dev

How can I obtain all of the file names in a directory?

From Dev

How to write out all directory names into a text file

From Dev

Remove all files of a type (file extension) from whole directory tree

From Dev

How to remove all files in a directory except one file in Solaris

From Dev

How to get all the matches from a file based on names in another file?

From Dev

Copy all files from subdirectories and subsubdirectories to main directory with modifying file names by adding prefix of all parent directories

From Dev

Remove MVC directory names from URL

From Dev

recursively remove newline from directory names

From Dev

Inno Setup: List all file names in an directory

From Dev

How do I remove Blank Space from File Names

From Dev

How to remove characters from file names using command line?

From Dev

Logback: How to remove class names and log level from Log file?

From Dev

How to remove the last line of all files from a directory?

From Dev

How to remove all folders named "Sample" from directory and subfolders?

From Dev

How to remove end of file names?

From Dev

How do I remove all files from wtihin a certain directory except for a child directory of that directory?

From Dev

How can i remove the file name from a path directory string?

From Dev

Windows 7:How to remove file types from all file explorer

Related Related

  1. 1

    How to remove files from a directory if their names are not in a text file? Bash script

  2. 2

    remove all leading and trailing whitespace from both file and directory names recursively

  3. 3

    remove date from file names for many files in a directory

  4. 4

    String from file names - How remove the extension?

  5. 5

    How to remove URI encoding from file names?

  6. 6

    How to remove a number from file names?

  7. 7

    How to remove all files from a directory?

  8. 8

    Subtract 1 from all file names (rename them) in a directory.

  9. 9

    Batch script to remove parentheses and spaces from all file names

  10. 10

    How to remove a directory path from a text file

  11. 11

    How to remove single file from /usr directory?

  12. 12

    How can I obtain all of the file names in a directory?

  13. 13

    How to write out all directory names into a text file

  14. 14

    Remove all files of a type (file extension) from whole directory tree

  15. 15

    How to remove all files in a directory except one file in Solaris

  16. 16

    How to get all the matches from a file based on names in another file?

  17. 17

    Copy all files from subdirectories and subsubdirectories to main directory with modifying file names by adding prefix of all parent directories

  18. 18

    Remove MVC directory names from URL

  19. 19

    recursively remove newline from directory names

  20. 20

    Inno Setup: List all file names in an directory

  21. 21

    How do I remove Blank Space from File Names

  22. 22

    How to remove characters from file names using command line?

  23. 23

    Logback: How to remove class names and log level from Log file?

  24. 24

    How to remove the last line of all files from a directory?

  25. 25

    How to remove all folders named "Sample" from directory and subfolders?

  26. 26

    How to remove end of file names?

  27. 27

    How do I remove all files from wtihin a certain directory except for a child directory of that directory?

  28. 28

    How can i remove the file name from a path directory string?

  29. 29

    Windows 7:How to remove file types from all file explorer

HotTag

Archive