I want to write a shell script that searches in any folder and handles any files that match the criteria.

Asked 2 years ago, Updated 2 years ago, 141 views

There are two things I want to do, but I'm having trouble writing well.
The following are the things I would like to deal with.

1

  • Search recursively for *.rar in folder A
  • Zip all *.rar files that hit the search
 if [the action to find *.rar]; then
    * The process of converting .rar to zip
fi

2

  • Search recursively for files with uppercase spaces in folder B
  • Remove uppercase file names from files that have hit the search
 if [Action to find blank characters];
  Delete blank characters in sed
fi

I would appreciate it if you could let me know if there is any good way to write.

For example, the directory structure is as follows:

animals  
├-- bird  
│   -- --bird_1.rar  
│   -- --bird_2.rar  
│   -- --bird_3.rar  
│   -- --bird_4.rar
│   -- --bird_5.rar
│   --penguin
│       -- --penguin_1.txt
│       -- --penguin_2.txt
│       -- --penguin_3.txt
│       -- --penguin_4.txt
│       ----penguin_5.txt
├-- cat
│   -- --cat_1.rar
│   -- --cat_2.rar
│   -- --cat_3.rar
│   -- --cat_4.rar
│   --cat_5.rar
└-- dog
    ├-- dog_1.rar
    ├-- dog_2.rar
    ├-- dog_3.rar
    ├-- dog_4.rar
    └-- dog_5.rar

bash shellscript

2022-09-30 21:28

3 Answers

Are you throwing away your work or class assignments? Tip only.

Simply display all *.rar files under the specified directory. Complete one task with a shell script that RAR→ZIP the echo "$rar" portion.

#!/bin/sh
find"$@"\
  -type f\
  -name '*.rar'\
  -exec sh-c\
    'for rar in "$@"; do echo "$rar"; done' \
    US>sh{}+\
;

Similarly, 2 is easy.

If it is uncomfortable to write shell scripts in a single quote in the -exec section of find, you can write them like this even though they are a little slow.An example that simply lists the name of a file under a specified directory that contains so-called full-width spaces (U+3000 IDEOGRAPHIC SPACE), but does not work if the file name contains a new line.

#!/bin/sh
find"$@"\
  -type f\
  -name '**'\
  -print\
| while IFS = read-r name; do
  echo "$name"
done


2022-09-30 21:28

How about this?
It's not recursive, but is it probably enough?

1
·Recursively search for *.rar in folder A
·Zip file conversion for all *.rar files that have been successfully searched

#!/bin/bash

# Run: ./rar2zip.sh [Folder Path]
#
# require the following commands
# realpath, unrar, zip, mktemp

function rar2zip {
    rar = "$(realpath"$1")"
    zip="$(realpath"${2:-$(basename"$rar".rar).zip}")"
    d=$(mktemp-d/tmp/rar2zip.XXXXXX)
    cd "$d"
    unrar x "$rar"
    zip-r "$zip" *
    mv-f$zip${rar%/*}
    cd-
    rm-r "$d"
}

# If you want to consider a blank file, leave out the following comments.
# SAVEIFS = $IFS
# IFS = $(echo-en "\n\b")

FILES=$(find${1}-name\*.rar-print)

for rar in ${FILES[@]}
do
    rar2zip "${rar}"
done
# IFS = $SAVEIFS

EDIT: Corresponding to blank characters pointed out in comments to @nekketsuuuu

I was writing the process using find and xargs, but it doesn't seem to work if the file path I found in the find command contains spaces.

Information related to the following sites

As you pointed out, it would be better to run with find...-exec+ or get the path with find...-print0.That's why when you grep-find with Emacs, it comes out with -print0.

Moreover, if a file contains spaces, the file path is stored in a blank space when filling the file path into an array of shells...this should certainly be done from find to -exec...

2
·Recursively search for files with capital spaces in the file name in folder B
·Delete uppercase spaces in the file name of the hit file

Remove whitespaces from filenames in Linux [closed]

#!/bin/bash

BLANK_FILES=$(find${1}-type f-print | grep' [[:blank:]]]')

for blank in $ {BLANK_FILES[@]}
do
    #echo$blank
    mv "$blank" "${blank//[:blank:]]}"
done

EDIT: Add what you provided in your comment

One-liner

find.-type f-name"*[[:blank:]]*"-exec bash-c'mv"$1""${1//[:blank:]]}"_{}\;


2022-09-30 21:28

After that, I will examine the information you gave me and use the sauce of the one liner below
myself. I thought about it, but is there any problem?

2
·Recursively search for files with capital spaces in the file name in folder B
·Delete uppercase spaces in the file name of the hit file

find.-name "*"-print 0 | xargs-0 names // "/


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.