LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2015, 12:07 PM   #1
mrquartz
LQ Newbie
 
Registered: Apr 2015
Posts: 9

Rep: Reputation: Disabled
Post \n newline character in cp command path


Hi guys,

I am writing a bash script with puppywary 5.3 distro. Basically, the script has to find certain files with extensions and move them to another directory.
Sounds easy, however, if there are files with the same name but different extensions only the one with .docx has to be copied. That's where the fun starts. When I try to do copy command with multiple files I get this:

cp: cannot stat `dir/filename\ndir/filename': no such file or directory

I know that \n is a newline character however I have no idea how does it get in between files when copying and whether its because flaws in code or because I am using distro. It concatenates both paths of files and prevents from copying.

Also, all the files I am copying are empty, using for test.

Thanks guys!

Code:
#!/bin/sh
src=$1
dst=$2
fdoc=$(find "$src" -type f -name "*.doc" 2>/dev/null)
fdocx=$(find "$src" -type f -name "*.docx" 2>/dev/null)
fpdf=$(find "$src" -type f -name "*.pdf" 2>/dev/null)
if [ ! -d "$src" ]; then
   echo "The path: $src does not exist. Please check the path"
   exit=1
fi
[ ! -d "$dst" ]
mkdir -p "$dst"
for i in "$src"; do
if [[ "$(basename "$fdoc" .doc)" == "$(basename "$fdocx" .docx)" ]] 
   cp "$fdocx" "$dst"
   fi
done
Please note: it is just the part of the code to compare two filenames and copy the .docx version.

Last edited by mrquartz; 04-13-2015 at 12:43 PM.
 
Old 04-13-2015, 12:17 PM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
I would guess there is a bug in your script - of course it is a guess since you haven't posted the script that is causing the error. Should you decide to post the code please use [code][/code] tags.
 
1 members found this post helpful.
Old 04-13-2015, 01:07 PM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Code:
fdoc=$(find "$src" -type f -name "*.doc" 2>/dev/null)
fdocx=$(find "$src" -type f -name "*.docx" 2>/dev/null)
fpdf=$(find "$src" -type f -name "*.pdf" 2>/dev/null)
This seems to break once there are multiple .doc, .docx or .pdf files in $src. Your script assumes that if there is any doc or docx file, there is exactly one.

Code:
[ ! -d "$dst" ]
This is pointless (unless you add set -e somewhere at the beginning).

Code:
for i in "$src"; do
This is equivalent to i=$src.

Code:
if [[ "$(basename "$fdoc" .doc)" == "$(basename "$fdocx" .docx)" ]]
Youre claiming to use /bin/sh so do so:
Code:
if [ "$(basename "$fdoc" .doc)" = "$(basename "$fdocx" .docx)" ]

Last edited by mina86; 04-13-2015 at 01:27 PM.
 
Old 04-13-2015, 01:16 PM   #4
mrquartz
LQ Newbie
 
Registered: Apr 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by mina86 View Post
Code:
fdoc=$(find "$src" -type f -name "*.doc" 2>/dev/null)
fdocx=$(find "$src" -type f -name "*.docx" 2>/dev/null)
fpdf=$(find "$src" -type f -name "*.pdf" 2>/dev/null)
This seems to break once there are multiple .doc, .docx or .pdf files in $src. Your script assumes that if there is any doc or docx file, there is exactly one.
Hey mina86 and thank you for your reply.
Any ideas how to fix my code ? I am relatively new to bash scripting.

However, when I use find command it gives me all files with .doc .docx or .pdf extensions.
 
Old 04-13-2015, 01:33 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
I usually just implement find in shell if I need to go through all files (with given extension) in a given directory and all its subdirectories. If you care about bash only, you can use extended globs like **/*.docx.
 
Old 04-13-2015, 01:37 PM   #6
mrquartz
LQ Newbie
 
Registered: Apr 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks for the idea, however, I believe problem is not in this this.
 
Old 04-13-2015, 02:08 PM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Im pretty sure the way you use find is the problem.
 
Old 04-13-2015, 02:34 PM   #8
mrquartz
LQ Newbie
 
Registered: Apr 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Could you then write the find, how you think it should be ?
 
Old 04-13-2015, 03:24 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Not sure I understand the problem. If the goal is to find all *.docx files and copy them to another directory then I do not understand the significance if there are also *.doc or *.pdf files of the same name or the necessity to compare file names.

Homework assignment?
 
Old 04-13-2015, 03:50 PM   #10
mrquartz
LQ Newbie
 
Registered: Apr 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Cool

Quote:
Originally Posted by michaelk View Post
Not sure I understand the problem. If the goal is to find all *.docx files and copy them to another directory then I do not understand the significance if there are also *.doc or *.pdf files of the same name or the necessity to compare file names.

Homework assignment?

Hey MichaelK and thanks for joining us.

The problem is to find all .doc, .docx and .pdf files and copy them to a directory. However, if names of files with .doc and .docx are the same then only .docx should be copied.

Yes it is! haha, how did you figure out that

Hope this makes it clearer.

Last edited by mrquartz; 04-13-2015 at 03:53 PM.
 
Old 04-13-2015, 07:58 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well the first thing that stood out to me was this:
Code:
if [ ! -d "$src" ]; then
   echo "The path: $src does not exist. Please check the path"
   exit=1
fi
You call this 'after' having already attempting to use $src in your find commands ... bit pointless after the fact.

Then as the question is homework, my suggestion would be:

1. Use find with -exec option to copy pdf and docx files to location

2. Pump find of doc files into a loop (while) and test the file name does not exist in the destination and then copy if not there


Note: Your current issue is with find as the default output from the find command is to place each found item on a separate line using newline. You could look into the -print option of find

Last edited by grail; 04-13-2015 at 08:00 PM.
 
Old 04-13-2015, 08:05 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
hint:
http://www.ibm.com/developerworks/ai...unix-find.html
 
Old 04-13-2015, 11:21 PM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
example
Code:
find . -iname '*.doc' -print | while read -r FILE; do
    process $FILE
done

Last edited by NevemTeve; 04-13-2015 at 11:32 PM.
 
Old 04-14-2015, 06:55 AM   #14
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Code:
find . -iname '*.doc' -print | while read -r FILE; do
Which of course breaks if there is a file with newline in its name, which is the reason I suggest implementing find in bash.

Quote:
Originally Posted by mrquartz View Post
The problem is to find all .doc, .docx and .pdf files and copy them to a directory. However, if names of files with .doc and .docx are the same then only .docx should be copied.
What if there is file with the same name in two different subdirectories of $src?

Last edited by mina86; 04-14-2015 at 06:56 AM.
 
Old 04-14-2015, 07:50 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> Which of course breaks if there is a file with newline in its name, which is the reason I suggest implementing find in bash.

Yes, those who have NL's in the file-names are in trouble...
Code:
find . -name '*.docx' -exec somecript {} \;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace the space with newline character after every nth field in a line surajchalukya Linux - Newbie 13 03-08-2013 09:58 PM
insert a newline after output command micronemo Linux - Newbie 1 09-13-2012 10:16 AM
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 08:41 PM
HP-UX newline character with sed jhwilliams Other *NIX 8 08-06-2007 05:13 PM
sed insert newline character jhwilliams Linux - Software 6 06-08-2007 03:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration