-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_spaces.sh
executable file
·50 lines (43 loc) · 1.02 KB
/
remove_spaces.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# remove spaces from filenames in current directory tree (just an example)
prepare_test()
{
TESTPATH=$(mktemp -d)
cd $TESTPATH
mkdir foo\ bar{0..9}
for dir in *; do
touch "$dir"/"f o o "{11..21}
done
touch file\ foo{0..9}
}
# this doesn't work (besides ignoring directories):
# find . -name *\ * -print0 | xargs --null -n 1 -I '{}' echo mv '{}' $(echo {} | sed 's/\ //g')
# the sed is called first and since {} expansion is done by xargs later, sed
# would have not effect on the string '{}' itself
do_remove()
{
find "$1" -depth -name '* *' -print0 \
| while IFS= read -r -d $'\0' PATHNAME; do
DIRNAME=$(dirname "$PATHNAME")
OLDNAME=$(basename "$PATHNAME")
NEWNAME=$(sed 's/\ //g' <<< "$OLDNAME")
mv "$PATHNAME" "${DIRNAME}/${NEWNAME}"
done
}
#
# main
#
if [[ x$1 = "x--prepare-test" ]]; then
prepare_test
echo $TESTPATH
exit
fi
if [[ x$1 = "x--test" ]]; then
prepare_test
do_remove "$TESTPATH"
echo $TESTPATH
exit
fi
for i; do
[[ -d "$i" ]] && do_remove "$i"
done