Change the encoding of every single .txt file in a folder and its subfolders

an offer can't refuse

I have a folder include many txt files and sub-folders which also contains many txt files.

I want to change all of the files' encoding automatically. Right now I know how to change one file using the command:

iconv -f gbk -t utf8 sample.txt > sample.wiki

I need the name of the file not changed and change its extension to .wiki. How can I achieve this automatically ?

heemayl

You can iterate though the .txt files, change encoding, save the output in .wiki file of the same name using -o option of iconv and if successful remove the relevant .txt file:

shopt -s globstar  ## Enables recursive glob matching
for f in **/*.txt; do
    iconv -f gbk -t utf8 -o "${f%.txt}".wiki "$f" && rm "$f"
done

Caveat:

If the input file is not in given source encoding or already in the target encoding, iconv will return true without doing any operation, hence the .wiki file will not be created and the .txt file will be removed.

To work around this you can save the STDOUT after conversion in a variable and if the variable is not-empty only then save the output in the .wiki file and remove the txt file:

shopt -s globstar  ## Enables recursive glob matching
for f in **/*.txt; do
    out="$(iconv -f gbk -t utf8 "$f")" 
    [[ -n $out ]] && echo "$out" >"${f%.txt}".wiki && rm "$f"
done

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I change permissions for a folder and all of its subfolders and files in one step in Linux?

From Java

Iterate through files in a folder and its subfolders using Swift's FileManager

From Dev

Run every file in a folder

From Dev

Command line to change file extension of all files under a folders and its subfolders

From Dev

Gulp - Target all files in a folder and its subfolders

From Dev

How to change permissions for a folder and its subfolders/files in one step?

From Dev

Using shell to modify files in a folder and all its subfolders

From Dev

Get path of all .txt files in a folder and subfolders

From Dev

Batch file to put files in folder (only subfolders)

From Dev

Change the encoding of every single .txt file in a folder and its subfolders

From Dev

Find and delete all files without extensions witin a folder and its subfolders

From Dev

Script to write all filenames inside a folder and its subfolders to a csv file

From Dev

Change owner of the root folder and subfolders (Ubuntu 13.04)

From Dev

Find and delete all files without extensions witin a folder and its subfolders

From Dev

Change encoding of txt file

From Dev

Script to write all filenames inside a folder and its subfolders to a csv file

From Dev

Change Text File Encoding

From Dev

Check and Delete specific file in a folder and subfolders

From Dev

Search and replace substring with an other in all the files of a folder and all its subfolders

From Dev

Validate/read total count of file in folder, size of every single file and some text of file with any extension on local folder

From Dev

Get path of all .txt files in a folder and subfolders

From Dev

Create folders and subfolders from csv/txt file

From Dev

Change multiple filenames by replacing a character in folder and subfolders

From Dev

Bash: Cleaning a folder and its subfolders of .log file

From Dev

vba that searches for a string in all the files inside a folder and its subfolders

From Dev

Read the first line of all txt files in a folder with subfolders and print filename and first line to another file

From Dev

Copying single file to selected subfolders

From Dev

Read any file with extension .txt inside a specified folder in ruby ​every 2 minutes

From Dev

Browse and merger Excel files in a folder and its subfolders in Python

Related Related

  1. 1

    How do I change permissions for a folder and all of its subfolders and files in one step in Linux?

  2. 2

    Iterate through files in a folder and its subfolders using Swift's FileManager

  3. 3

    Run every file in a folder

  4. 4

    Command line to change file extension of all files under a folders and its subfolders

  5. 5

    Gulp - Target all files in a folder and its subfolders

  6. 6

    How to change permissions for a folder and its subfolders/files in one step?

  7. 7

    Using shell to modify files in a folder and all its subfolders

  8. 8

    Get path of all .txt files in a folder and subfolders

  9. 9

    Batch file to put files in folder (only subfolders)

  10. 10

    Change the encoding of every single .txt file in a folder and its subfolders

  11. 11

    Find and delete all files without extensions witin a folder and its subfolders

  12. 12

    Script to write all filenames inside a folder and its subfolders to a csv file

  13. 13

    Change owner of the root folder and subfolders (Ubuntu 13.04)

  14. 14

    Find and delete all files without extensions witin a folder and its subfolders

  15. 15

    Change encoding of txt file

  16. 16

    Script to write all filenames inside a folder and its subfolders to a csv file

  17. 17

    Change Text File Encoding

  18. 18

    Check and Delete specific file in a folder and subfolders

  19. 19

    Search and replace substring with an other in all the files of a folder and all its subfolders

  20. 20

    Validate/read total count of file in folder, size of every single file and some text of file with any extension on local folder

  21. 21

    Get path of all .txt files in a folder and subfolders

  22. 22

    Create folders and subfolders from csv/txt file

  23. 23

    Change multiple filenames by replacing a character in folder and subfolders

  24. 24

    Bash: Cleaning a folder and its subfolders of .log file

  25. 25

    vba that searches for a string in all the files inside a folder and its subfolders

  26. 26

    Read the first line of all txt files in a folder with subfolders and print filename and first line to another file

  27. 27

    Copying single file to selected subfolders

  28. 28

    Read any file with extension .txt inside a specified folder in ruby ​every 2 minutes

  29. 29

    Browse and merger Excel files in a folder and its subfolders in Python

HotTag

Archive