List paths with find when the filename contains spaces [duplicate] - linux

This question already has answers here:
Iterate over a list of files with spaces
(12 answers)
Closed 9 years ago.
I have this code
for i in $(find pwd)
do
echo $i
done
the problem is if the file name contains spaces, it prints on a separate line
how can I list all of the files in some directory including files that contains spaces

This would have the intended effect of your example:
find /path/to/somewhere
That is, no need to wrap a for loop around it.
But I'm guessing you want something more than just echoing. Perhaps call a script for each file? You can do that with:
find /path/to/somewhere -exec path/to/script.sh {} \;
where {} will be replaced for each filename found.

I would use while read for this.
find . | while read i; do echo $i; done;
Edit:
Alternatively, you could just do ls -a1

here is the solution
IFS=$'\n'
for i in $(pwd)
do
echo $i
done

Related

Writing a bash script to find all files in a directory that start with a, and do nothing if one exist [duplicate]

This question already has answers here:
Do not show results if directory is empty using Bash
(3 answers)
Closed 4 years ago.
So I have to find all the files in the directory that start with the letter a, and list them out. This is pretty easy by doing
cd some_directory
for file in a*; do
echo "$file"
done
However I want that if there are no files present that match a*, then the for loop will not run at all. Currently, if this is the case then the shell will echo
a*
Is there a way to do this? Thank you
Your text is opposite of your title, in my answer below I've assumed the text is your intention and your title is incorrect:
globs can be made to act like this with the bash shell option "nullglob":
shopt -s nullglob
An alternative is to use find and ignore errors by piping stderr to /dev/null
for file in $(find a* 2>/dev/null); do
echo "$file"
done

For loop in bash [duplicate]

This question already has answers here:
How to loop through a directory recursively to delete files with certain extensions
(16 answers)
BASH: Writing a Script to Recursively Travel a Directory of N Levels
(2 answers)
Closed 4 years ago.
I'm trying to write a bash script that recursively goes through files in a directory, writing the file's name and hexdump to a file. My current script:
#/bin/sh
touch hexdump.txt
for filename in logical/*; do
echo "$filename"
"$filename" >> hexdump.txt
hd /logical/"$filename" >> hexdump.txt
done
The current output is:
logical/*
./hexadecimalExtraction.sh: line 5: logical/*: No such file or directory
hd: /logical/logical/*: No such file or directory
How do i get it to interpret "logical/*" as the list of files within "logical" directory and not the filename itself???
"$filename" >> hexdump.txt
should probably be removed
Otherwise you are trying to run the filename itself.
Also you are looking for files in logical subdirectory in the current directory, but the trying to look in /logical/
You can't recurse with for filename in logical/*. In order to recurse, you have to use find.
To make find visit only files, not directories, use find -type f.
I don't know hd, but you probably want
find tutorials -type f | while read i; do
echo $i >> hexdump.txt
hd $i >> hexdump.txt
done
You're looking for the ** glob operator.
shopt -s globstar nullglob
for filename in logical/**/*; do
echo "$filename"
hd "$filename"
done >> hexdump.txt
filename will contain the full name of the matched files, which already includes the directory logical and any sub directories.

star wildcard in bash [duplicate]

This question already has answers here:
Rename multiple files in shell [duplicate]
(4 answers)
Closed 4 years ago.
I've got a small problem with my bash script. I try to change file name in current directory for whole files with txt extension to text extension. For exampel 1.txt to 1.text
My script looks like this now:
#!/bin/bash
FILES=`ls /home/name/*.txt`
NAME=*.txt
RENAME=*.text
for file in FILES
do
mv $NAME $RENAME
done
i try whole combination with single, double quotes and backticks and I receive errors all the time.
Do you have some ideas how to receive wildcards "*" in bash?
Thanks.
That's not at all how you do that.
#!/bin/bash
shopt -s nullglob
OLD=.txt
NEW=.text
FILES=(/home/name/*"$OLD")
for file in "${FILES[#]}"
do
mv "$file" "${file%$OLD}$NEW}"
done
There are a number of issues with your script. Firstly, you shouldn't run ls and attempt to store its output like that. If you want to iterate through those file, just do it in the loop:
for file in /home/name/*.txt
Now the shell is doing all the work for you, and as a bonus handling any kind of weird filenames that you might have.
In your example you were looping over the literal string "FILES", not the variable, but I guess that was just a typo.
The built-in way to change the filename is to use a parameter expansion to remove the old one, then concatenate with the new one:
old=txt
new=text
for file in /home/name/*"$old"
do
mv "$file" "${file%$old}$new"
done
If it is possible that there are no files matching the glob, then by default, the /home/name/*.txt will not be expanded and your loop will just run once. then you have a couple of options:
use shopt -s nullglob so that /home/name/*.txt expands to null, and the loop is skipped
add an explicit check inside the loop to ensure that $file exists before trying to mv:
for file in /home/name/*"$old"
do
[ -e "$file" ] || continue
mv "$file" "${file%$old}$new"
done
You can use rename to rename filenames.
rename .txt .text /home/name/*.txt
And if you want to do this by looping, you can
for FILE in /data/tmp/*.txt; do
mv "${FILE}" "${FILE/.txt/.text}"
done

shell script to list file names alone in a directory & rename it [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 6 years ago.
I'm new to scripting concept.. I have a requirement to rename multiple files in a directory like filename.sh.x into filename.sh
First I tried to get the file names in a particular directory.. so i followed the below scripting code
for entry in PathToThedirectory/*sh.x
do
echo $entry
done
& the above code listed down all the file names with full path..
But my expected o/p is : to get file names alone like abc.sh.x,
so that I can proceed with the split string mechanism to perform rename
operation easily...
help me to solve this ... Thanks in advance
First approach trying to follow OP suggestions:
for i in my/path/*.py.x
do
basename=$(basename "$i")
mv my/path/"$basename" my/path/"${basename%.*}"
done
And maybe, you can simplify it:
for i in my/path/*.py.x
do
mv "$i" "${i%.*}";
done
Documentation regarding this kind of operation (parameter expansion): https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
In particular:
${parameter%word} : The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted
So, ${i%.*} means:
Take $i
Match .* at the end of its value (. being a literal character)
Remove the shortest matching pattern
Look into prename (installed together with the perl package on ubuntu).
Then you can just do something like:
prename 's/\.x$//' *.sh.x
In ksh you can do this:
for $file in $(ls $path)
do
new_file=$(basename $path/$file .x)
mv ${path}/${file} ${path}/${new_file}
done
This should do the trick:
for file in *.sh.x;
do
mv "$file " "${file /.sh.x}";
done
Running this rename command from the root directory should work:
rename 's/\.sh\.x$/.sh/' *.sh.x
for i in ls -la /path|grep -v ^d|awk '{print $NF}'
do
echo "basename $i"
done
it will give u the base name of all files or you can try below
find /path -type f -exec basename {} \;

Change file names iteratively in Linux [duplicate]

This question already has answers here:
Rename multiple files in bash
(3 answers)
Closed 8 years ago.
The only way I am aware of to do this operation is with a for loop iterating over each file:
for file in *something.txt; do
out=\`echo $file | sed 's/something/else/'\`; mv $file $out;
done
I was wondering if there is any other way or shortcut for it (using GNU bash).
There is also simple substring replacement provided as part of bash itself:
mv $file ${file/something/else}
example:
$ touch {1..3}something.txt
$ ( for i in *something.txt; do mv $i ${i/something/else}; done )
$ ls -1 *else*
1else.txt
2else.txt
3else.txt
There's rename and the same basic loop concept as in your post only in whatever programming language you choose.

Resources