Chmod with spaces and wildcards


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Chmod with spaces and wildcards
# 1  
Old 10-14-2016
Chmod with spaces and wildcards

Hello all,

I have a Problem within my Shell-script to do a 'chmod' at some files - where the Directory has spaces. Because I want to do this for many files I use the '*' Wildcard.

Here is my script:

Code:
#!/bin/bash
 str="/home/hte4993/tmp/Porg Dir/t_*.txt"
 neu=$(echo "$str" | sed 's/ /\\ /g')
 chmod 775 $neu

Quote:
chmod: /home/hte4993/tmp/Porg\: No such file or directory
chmod: Dir/t_*.txt: No such file or Directory
And the script with qoutes around the variable:

Code:
#!/bin/bash
 str="/home/hte4993/tmp/Porg Dir/t_*.txt"
 neu=$(echo "$str" | sed 's/ /\\ /g')
 chmod 775 "$neu"

Quote:
chmod: /home/hte4993/tmp/Porg\ Dir/t_*.txt: No such file or directory
Do you have a solution for this?

CU
# 2  
Old 10-14-2016
Hi,
Your problem is not spaces but the not eval of '*'...
Try:
Code:
#!/bin/bash
str="/home/hte4993/tmp/Porg Dir/t_"*.txt
chmod 775 "$str"

But you must have just only one file either you must use a loop.
Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 10-14-2016
@disedorgue: This does not work on my side. I will get the message, that no files have been found - but there are files in.

In the meantime I tried some things, and I found the solution:

Code:
#!/bin/bash
 str="/home/hte4993/tmp/Porg Dir/t_*.txt"
find "$(dirname "$str")" -name "$(basename "$str")" 2>/dev/null | while read file
do
  chmod 775 "$file"
done

This is working for me.

Nevertheless many thanks.
# 4  
Old 10-14-2016
If you have a huge list of files to process, the following should work just as well:
Code:
#!/bin/bash
str="/home/hte4993/tmp/Porg Dir/t_*.txt"
find "${str%/*}" -name "${str##*/}" -exec chmod 775 {} +

If you don't have a huge list of file to process, the following would be simpler:
Code:
#!/bin/bash
str="/home/hte4993/tmp/Porg Dir/t_*.txt"
cd "${str%/*}"
chmod 775 ${str##*/}

but this latter form will print a diagnostic similar to:
Code:
chmod: t_*.txt: No such file or directory

if no matching files are present in that directory.

Or, if the list of files is relatively short, just:
Code:
chmod 775 "/home/hte4993/tmp/Porg Dir/t_"*.txt

Note that the asterisk must be been moved outside the double-quotes for this to work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Wildcards

These 2 websites do a GREAT job of explaining different types of wildcards. I learned about the categories of characters which I never knew about at all. GNU/Linux Command-Line Tools Guide - Wildcards GREP (1 Reply)
Discussion started by: cokedude
1 Replies

2. UNIX and Linux Applications

What is the difference between chmod in solaris and chmod in Linux?

i think it is the same in both... Iam i right? (1 Reply)
Discussion started by: sumaiya
1 Replies

3. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

4. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

5. UNIX for Dummies Questions & Answers

wildcards NOT

Hi All Please excuse another straightforward question. When creating a tar archive from a directory I am attempting to use wildcards to eliminate certain filetypes (otherwise the archive gets too large). So I am looking for something along these lines. tar -cf archive.tar * <minus all *.rst... (5 Replies)
Discussion started by: C3000
5 Replies

6. UNIX for Dummies Questions & Answers

ls with wildcards

ok, I'm trying to write a script file that lists files with specific elements in the name into a txt file, it looks like this ls s*.dat > file_names.txt can't figure out whats wrong with that line, any ideas? thanks in advance (10 Replies)
Discussion started by: benu302000
10 Replies

7. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

8. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

9. UNIX for Dummies Questions & Answers

Wildcards and quoting

Hi All In a script, I want a user to enter 4 characters, these can be a mix of letters (uppercase and lowercase) and numbers. In this example $var represents what the user has entered. eg $var can be A9xZ, 3DDL, bbHp .........etc I need to check that the user has only entered characters... (2 Replies)
Discussion started by: Bab00shka
2 Replies

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question