Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Question/review my script: removing bad chars from filenames Post 302459136 by uiop44 on Sunday 3rd of October 2010 09:53:30 PM
Old 10-03-2010
Question/review my script: removing bad chars from filenames

The task: remove undesirable characters from filenames.

Restrictions: Must use basic RE, base utilities (non-GNU) and /bin/sh (ash). No ksh, zsh, perl, etc.

Below is what I've come up with. It seems to work OK but I'm open to shorter, more efficient alternatives.

Inside the square brackets I've included an assortment of chars I'd like to remove. But I'm not able to get the following chars inside the square brackets so that they are also removed:

- square brackets
- single quotes (apostrophes)

One solution is to pipe into another sed. But I'd like to avoid that. My version of sed does not accept hex or octal.

Code:
# these are for debugging; they just print out the command line 

nsc(){ 
IFS='
';for j in $@ ;do printf "mv -vi \"$j\" \134 \n \"$j\"\n" $@;done | sed '/ \\ $/!s/[[:cntrl:][:blank:]\(\)\{\}:;,!?+*=<>#@\^|]//g;s/\]//g'  ;}

# problem: does not remove square brackets and single quotes

nsc(){ IFS='
';for j in $@ ;do printf "mv -vi \"$j\" \134 \n \"$j\"\n" $@;done | sed 's/ \\ $/!s/[[:cntrl:][:blank:]\(\)\{\}:;,!?+*=<>#@\^|]//g;s/\]//g;s/\[//g' | sed "s/\'//g;s/\[//g;s/\]//g" ;}

# removes single quotes and square brackets but uses an extra sed

Note: I have tried the "while read" approach but I found that the read builtin required filenames that do not contain illegal characters for variables, such as parentheses.

Last edited by uiop44; 10-03-2010 at 11:02 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

removing the extension from all filenames in a folder

Hi there, I'm pretty new to UNIX and have tried trawling through this forum to find an answer to what I want to try to do, which I'm sure is very simple but I don't know how to do it. What I have a a folder that contains multiple files that I have copied from Windows and I want to remove the... (5 Replies)
Discussion started by: johnmcclintock
5 Replies

2. Shell Programming and Scripting

removing non-printable chars from multiple files

How do I remove non-printable characters from all txt files and output the results to one file? I've tried the following: tr -cd '\n' < *.txt > out.txt and it gives ambiguous redirect error. How can I get it to operate on all txt files in the current directory and append the output to... (1 Reply)
Discussion started by: revax
1 Replies

3. Shell Programming and Scripting

SED: Removing Filenames From Paths

I'm using a script with a lot of SED commands, in conjunction with grep, cut, etc. I've come up against a wall with a particular road block: I output a file from an SVN registry that gives me a list of files. The list consists of a variable number of lines that contain a path/file. The paths... (4 Replies)
Discussion started by: Brusimm
4 Replies

4. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

5. Shell Programming and Scripting

removing file with bad characters

I have the following files in the same directory but if you look at the od output you can see one of the files has and "\n" as part of the file name. Is there a way I can only remove the file with the "\n" as part of the file name without affecting the other file. I was thinking about... (4 Replies)
Discussion started by: BeefStu
4 Replies

6. Shell Programming and Scripting

Removing unknow chars from file names.

I'm trying to move a large folder to an external drive but some files have these weird chars that the external drive won't accept. Does anyone know any command of any bash script that will look through a given folder and remove any weird chars? (4 Replies)
Discussion started by: Joktaa
4 Replies

7. Shell Programming and Scripting

Removing rows and chars from text file

Dear community, maybe I'm asking the moon :rolleyes:, but I'm scratching my head to find a solution for it. :wall: I have a file called query.out (coming from Oracle query), the file is like this: ADDR TOTAL -------------------- ---------- TGROUPAGGR... (16 Replies)
Discussion started by: Lord Spectre
16 Replies

8. UNIX for Advanced & Expert Users

Removing special chars from file and maintain field separator

Running SunOs 5.6. Solaris. I've been able to remove all special characters from a fixed length file which appear in the first column but as a result all subsequent columns have shifted to the left by the amount of characters deleted. It is a space separated file. Line 1 in input file is... (6 Replies)
Discussion started by: iffy290
6 Replies

9. UNIX for Beginners Questions & Answers

Change encoding, no removing special chars. inconv

Hi all, I'm using iconv command to change files encoding to UTF-8 If my input file has chars as those are removed creating the file without those special chars. I tried using iconv -c, but there is still the removal. Is there a way to keep those special chars changing just the... (6 Replies)
Discussion started by: mrreds
6 Replies

10. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies
cronjob_selinux(8)					      SELinux Policy cronjob						cronjob_selinux(8)

NAME
cronjob_selinux - Security Enhanced Linux Policy for the cronjob processes DESCRIPTION
Security-Enhanced Linux secures the cronjob processes via flexible mandatory access control. The cronjob processes execute with the cronjob_t SELinux type. You can check if you have these processes running by executing the ps com- mand with the -Z qualifier. For example: ps -eZ | grep cronjob_t ENTRYPOINTS
The cronjob_t SELinux type can be entered via the user_cron_spool_t, shell_exec_t file types. The default entrypoint paths for the cronjob_t domain are the following: /var/spool/at(/.*)?, /var/spool/cron, /bin/d?ash, /bin/zsh.*, /bin/ksh.*, /usr/bin/d?ash, /usr/bin/zsh.*, /usr/bin/ksh.*, /bin/esh, /bin/mksh, /bin/sash, /bin/tcsh, /bin/yash, /bin/bash, /bin/fish, /bin/bash2, /usr/bin/esh, /usr/bin/sash, /usr/bin/tcsh, /usr/bin/yash, /usr/bin/fish, /usr/bin/mksh, /usr/bin/bash, /sbin/nologin, /usr/sbin/sesh, /usr/bin/bash2, /usr/sbin/smrsh, /usr/bin/scponly, /usr/sbin/nologin, /usr/libexec/sesh, /usr/sbin/scponlyc, /usr/bin/git-shell, /usr/libexec/git-core/git-shell PROCESS TYPES
SELinux defines process types (domains) for each process running on the system You can see the context of a process using the -Z option to ps Policy governs the access confined processes have to files. SELinux cronjob policy is very flexible allowing users to setup their cronjob processes in as secure a method as possible. The following process types are defined for cronjob: cronjob_t Note: semanage permissive -a cronjob_t can be used to make the process type cronjob_t permissive. SELinux does not deny access to permis- sive process types, but the AVC (SELinux denials) messages are still generated. BOOLEANS
SELinux policy is customizable based on least access required. cronjob policy is extremely flexible and has several booleans that allow you to manipulate the policy and run cronjob with the tightest access possible. If you want to deny any process from ptracing or debugging any other processes, you must turn on the deny_ptrace boolean. Enabled by default. setsebool -P deny_ptrace 1 If you want to allow all domains to use other domains file descriptors, you must turn on the domain_fd_use boolean. Enabled by default. setsebool -P domain_fd_use 1 If you want to allow all domains to have the kernel load modules, you must turn on the domain_kernel_load_modules boolean. Disabled by default. setsebool -P domain_kernel_load_modules 1 If you want to allow all domains to execute in fips_mode, you must turn on the fips_mode boolean. Enabled by default. setsebool -P fips_mode 1 If you want to enable reading of urandom for all domains, you must turn on the global_ssp boolean. Disabled by default. setsebool -P global_ssp 1 If you want to allow system to run with NIS, you must turn on the nis_enabled boolean. Disabled by default. setsebool -P nis_enabled 1 MANAGED FILES
The SELinux process type cronjob_t can manage files labeled with the following file types. The paths listed are the default paths for these file types. Note the processes UID still need to have DAC permissions. user_home_t /home/[^/]*/.+ user_tmp_t /var/run/user(/.*)? /tmp/hsperfdata_root /var/tmp/hsperfdata_root /tmp/gconfd-.* COMMANDS
semanage fcontext can also be used to manipulate default file context mappings. semanage permissive can also be used to manipulate whether or not a process type is permissive. semanage module can also be used to enable/disable/install/remove policy modules. semanage boolean can also be used to manipulate the booleans system-config-selinux is a GUI tool available to customize SELinux policy settings. AUTHOR
This manual page was auto-generated using sepolicy manpage . SEE ALSO
selinux(8), cronjob(8), semanage(8), restorecon(8), chcon(1), sepolicy(8) , setsebool(8) cronjob 14-06-10 cronjob_selinux(8)
All times are GMT -4. The time now is 06:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy