Checking File Sums between 2 directories
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I would like to check the file check sums between 2 directories.
I have written the following script to do so:
#!/bin/bash
echo Checking File Copy
SRC_DIR=/home/user/src
echo Source Directory: $SRC_DIR
TGT_DIR=/media/user/tgt
echo Target Directory: $TGT_DIR
cd $SRC_DIR
for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done
The script works fine until it encounters a file with space in the name at which point I get an error.
I tried to solve it by "escaping" the file name like so:
tgt_file=$file_name// /\
Unfortunately that causes an error when reading the file names.
I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.
bash ubuntu filenames
add a comment |Â
up vote
1
down vote
favorite
I would like to check the file check sums between 2 directories.
I have written the following script to do so:
#!/bin/bash
echo Checking File Copy
SRC_DIR=/home/user/src
echo Source Directory: $SRC_DIR
TGT_DIR=/media/user/tgt
echo Target Directory: $TGT_DIR
cd $SRC_DIR
for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done
The script works fine until it encounters a file with space in the name at which point I get an error.
I tried to solve it by "escaping" the file name like so:
tgt_file=$file_name// /\
Unfortunately that causes an error when reading the file names.
I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.
bash ubuntu filenames
2
Why don't you just double quote the variable expansions instead?
â Kusalananda
Nov 11 '17 at 16:22
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I would like to check the file check sums between 2 directories.
I have written the following script to do so:
#!/bin/bash
echo Checking File Copy
SRC_DIR=/home/user/src
echo Source Directory: $SRC_DIR
TGT_DIR=/media/user/tgt
echo Target Directory: $TGT_DIR
cd $SRC_DIR
for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done
The script works fine until it encounters a file with space in the name at which point I get an error.
I tried to solve it by "escaping" the file name like so:
tgt_file=$file_name// /\
Unfortunately that causes an error when reading the file names.
I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.
bash ubuntu filenames
I would like to check the file check sums between 2 directories.
I have written the following script to do so:
#!/bin/bash
echo Checking File Copy
SRC_DIR=/home/user/src
echo Source Directory: $SRC_DIR
TGT_DIR=/media/user/tgt
echo Target Directory: $TGT_DIR
cd $SRC_DIR
for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done
The script works fine until it encounters a file with space in the name at which point I get an error.
I tried to solve it by "escaping" the file name like so:
tgt_file=$file_name// /\
Unfortunately that causes an error when reading the file names.
I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.
bash ubuntu filenames
asked Nov 11 '17 at 16:20
earnshae
61
61
2
Why don't you just double quote the variable expansions instead?
â Kusalananda
Nov 11 '17 at 16:22
add a comment |Â
2
Why don't you just double quote the variable expansions instead?
â Kusalananda
Nov 11 '17 at 16:22
2
2
Why don't you just double quote the variable expansions instead?
â Kusalananda
Nov 11 '17 at 16:22
Why don't you just double quote the variable expansions instead?
â Kusalananda
Nov 11 '17 at 16:22
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.
For further discussion on why this is important see the following post:
- Why does my shell script choke on whitespace or other special characters?
Here is what your script should probably look like:
#!/bin/bash
echo "Checking File Copy"
SRC_DIR="/home/user/src"
echo "Source Directory: $SRC_DIR"
TGT_DIR="/media/user/tgt"
echo "Target Directory: $TGT_DIR"
cd "$SRC_DIR"
for file_name in *.txt
do
echo "$file_name"
sum "$TGT_DIR/$file_name"
sum "$file_name"
done
You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.
I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:
- When do we need curly braces around shell variables?
add a comment |Â
up vote
0
down vote
diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)
ls -1 gives you a list of files, 1 each line
wc -l count the lines
EDIT:
for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.
For further discussion on why this is important see the following post:
- Why does my shell script choke on whitespace or other special characters?
Here is what your script should probably look like:
#!/bin/bash
echo "Checking File Copy"
SRC_DIR="/home/user/src"
echo "Source Directory: $SRC_DIR"
TGT_DIR="/media/user/tgt"
echo "Target Directory: $TGT_DIR"
cd "$SRC_DIR"
for file_name in *.txt
do
echo "$file_name"
sum "$TGT_DIR/$file_name"
sum "$file_name"
done
You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.
I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:
- When do we need curly braces around shell variables?
add a comment |Â
up vote
3
down vote
You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.
For further discussion on why this is important see the following post:
- Why does my shell script choke on whitespace or other special characters?
Here is what your script should probably look like:
#!/bin/bash
echo "Checking File Copy"
SRC_DIR="/home/user/src"
echo "Source Directory: $SRC_DIR"
TGT_DIR="/media/user/tgt"
echo "Target Directory: $TGT_DIR"
cd "$SRC_DIR"
for file_name in *.txt
do
echo "$file_name"
sum "$TGT_DIR/$file_name"
sum "$file_name"
done
You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.
I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:
- When do we need curly braces around shell variables?
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.
For further discussion on why this is important see the following post:
- Why does my shell script choke on whitespace or other special characters?
Here is what your script should probably look like:
#!/bin/bash
echo "Checking File Copy"
SRC_DIR="/home/user/src"
echo "Source Directory: $SRC_DIR"
TGT_DIR="/media/user/tgt"
echo "Target Directory: $TGT_DIR"
cd "$SRC_DIR"
for file_name in *.txt
do
echo "$file_name"
sum "$TGT_DIR/$file_name"
sum "$file_name"
done
You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.
I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:
- When do we need curly braces around shell variables?
You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.
For further discussion on why this is important see the following post:
- Why does my shell script choke on whitespace or other special characters?
Here is what your script should probably look like:
#!/bin/bash
echo "Checking File Copy"
SRC_DIR="/home/user/src"
echo "Source Directory: $SRC_DIR"
TGT_DIR="/media/user/tgt"
echo "Target Directory: $TGT_DIR"
cd "$SRC_DIR"
for file_name in *.txt
do
echo "$file_name"
sum "$TGT_DIR/$file_name"
sum "$file_name"
done
You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.
I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:
- When do we need curly braces around shell variables?
edited Nov 11 '17 at 16:32
answered Nov 11 '17 at 16:26
igal
4,830930
4,830930
add a comment |Â
add a comment |Â
up vote
0
down vote
diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)
ls -1 gives you a list of files, 1 each line
wc -l count the lines
EDIT:
for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done
add a comment |Â
up vote
0
down vote
diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)
ls -1 gives you a list of files, 1 each line
wc -l count the lines
EDIT:
for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done
add a comment |Â
up vote
0
down vote
up vote
0
down vote
diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)
ls -1 gives you a list of files, 1 each line
wc -l count the lines
EDIT:
for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done
diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)
ls -1 gives you a list of files, 1 each line
wc -l count the lines
EDIT:
for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done
edited Nov 11 '17 at 21:05
answered Nov 11 '17 at 20:46
FaxMax
428219
428219
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403923%2fchecking-file-sums-between-2-directories%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Why don't you just double quote the variable expansions instead?
â Kusalananda
Nov 11 '17 at 16:22