25 Juli 2013

Menghilangkan karakter aneh pada berkas

Walaupun GNU/Linux dapat mengenal nama berkas dengan karakter aneh-aneh, hal tersebut dapat mengganggu bagi pengguna "command line". Untuk itu, berikut sebuah script sederhana yang mengganti karakter-karakter aneh seperti [] (),?*#" menjadi dash "-". Jika ingin menghilangkan karakter lain, silakan modifikasi bagian "gsub()".

Selain itu, mode berkas menjadi 644 dan mode direktori menjadi 755. Script bernama "fixfs" ini menggunakan bash, ls, dan gawk.


#!/bin/bash
# REV20: Fri 16 Sep 2022 17:00
# REV19: Sun 23 Feb 2020 15:00
# REV09: Thu  6 Feb 2020 23:00
# REV07: Mon 27 Aug 2018 20:00
# REV05: Fri  8 Sep 2017 20:00
# START: Thu 19 Dec 2013 00:00

# Copyright (C) 2013-2022 BinKadal, Sdn. Bhd.
# This program is free script/software. This program is distributed in the hope 
# that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# Add escape \"
# Replacing all funny and white characters with a dash (-).
# Replacing multiple dashes (---) with a single dash (-)
# No dash before a dot.

function recursive() {
   ls -1 | gawk '{
      FNAME1=$0
      gsub (/"/,"\\\"",FNAME1) 
      FNAME2=FNAME1
      NCHG =  0
      NCHG += gsub (/[\\\*\?\[\]\047":,!( )^@&#]/,"-",FNAME2) 
      NCHG += gsub (/\.-/,"-",FNAME2) 
      NCHG += gsub (/--+/,"-",FNAME2) 
      NCHG += gsub (/_-/,"-",FNAME2) 
      NCHG += gsub (/-_/,"-",FNAME2) 
      NCHG += gsub (/-\./,".",FNAME2) 
      NCHG += gsub (/\._/,"_",FNAME2) 
      NCHG += gsub (/__+/,"_",FNAME2) 
      NCHG += gsub (/_\./,".",FNAME2) 
      NCHG += gsub (/\.\.+/,".",FNAME2) 
      NCHG += gsub (/^-/,"",FNAME2) 
      NCHG += gsub (/-$/,"",FNAME2) 
      if (NCHG > 0) {
         EXTRA=""
         TMPF=FNAME2 EXTRA
         while ((getline < TMPF) >= 0) {
            EXTRA++
            TMPF=FNAME2 "-" EXTRA
         }
         system("/bin/mv -f \""  FNAME1  "\" " TMPF)
      }
   }'

   # chmod 755 for all directories
   # chmod 644 for all files
   for II in {.??,}*
   do
      [ -d "$II" ] && [ ! -L "$II" ] && {
         chmod 755 "$II"
         cd "$II"
         # Recursive inside the DIR
         recursive
         cd ..
      }
      [ -f "$II" ] && chmod 644 "$II"
   done
}

recursive
echo "Check $0"

exit

###############################
# TAKE NOTE (WK16)
#
# this file will fix name of file that contain irregular name



DISCLAIMER


This is HOW Me Do IT! Grrr... this blog memo is mainly written for OWN PURPOSES. This post is based on "Google Here, There, and Everywhere". Whether this is PLAGIARY or RESEARCH, there has never been a claim that this is an original work, nor is it necessarily the best solution, and not for Scopus consumption :). Please provide feedback, especially if you have alternative explanations. Hopefully, this note will be helpful in the future when you have forgotten how to solve this trivia problem.


DISKLAIMER


INIlah yang KUlakukan! Grrr... memo blog ini terutama ditulis untuk KEPERLUAN SENDIRI. Tulisan ini berbasis "Google Sana, Google Sini, Coba Itu, Coba Ini, Lalu Tanya-tanyi". Entah ini PLAGIAT, entah ini RISET, yang jelas tidak pernah ada klaim bahwa ini merupakan karya asli, serta belum tentu pula merupakan solusi terbaik, serta bukan untuk konsumsi Scopus :). Mohon kiranya memberikan tanggapan, terutama jika memiliki solusi alternatif. Semoga catatan ini akan bermanfaat di masa mendatang, saat sudah lupa cara menyelesaikan masalah trivia ini.

This is the Way!

Tidak ada komentar:

Posting Komentar