LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2019, 09:59 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 307

Rep: Reputation: 52
Insert word between filename.


Hello

How to insert a word in between a filename with spaces while cp or mv. For example in the following two examples, I do as follows:

Code:
# ls -l /etc/vsftpd/vsftpd.conf*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf

# cp /etc/vsftpd/vsftpd.conf{,.backup}

# ls -l /etc/vsftpd/vsftpd.conf*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf
-rw-------. 1 root root 5113 Dec 27 20:12 /etc/vsftpd/vsftpd.conf.backup
Code:
# ls -l /etc/vsftpd/vsftpd*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf

# cp /etc/vsftpd/vsftpd{,.PREFIX}.conf

# ls -l /etc/vsftpd/vsftpd*
-rw-------. 1 root root 5113 Dec 24 18:30 /etc/vsftpd/vsftpd.conf
-rw-------. 1 root root 5113 Dec 27 20:15 /etc/vsftpd/vsftpd.PREFIX.conf
Following this method, how to insert some chars between filename with spaces.

For example, I want to copy the file 'A Sample File.txt' as 'A Sample TEST File.txt'

I tried these, won't work.

Code:
# cp 'A Sample {,TEST} File.txt' 
cp: missing destination file operand after 'A Sample {,TEST} File.txt'

# cp 'A Sample \{,TEST\} File.txt' 
cp: missing destination file operand after 'A Sample \{,TEST\} File.txt'
Thanks
 
Old 12-27-2019, 10:53 AM   #2
fatmac
Senior Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Posts: 3,879

Rep: Reputation: Disabled
Linux doesn't like spaces in file names, it's a separator.
Replacing the space with an underscore is the easiest way.
Your problem is mainly owing to a lack of destination file, mv & cp, from/to.
Code:
mv A_Sample_File.txt A_Sample_TEST_File.txt
 
Old 12-27-2019, 11:18 AM   #3
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 781

Rep: Reputation: 560Reputation: 560Reputation: 560Reputation: 560Reputation: 560Reputation: 560
The reason why what you tried is erroring is because brace expansion doesn't occur inside single-quoted strings, meaning you're only passing a single argument (which is why the messages complains about a lack of destination).

Remove the quotes and backslash escape the spaces and you will get the behaviour you're after -- however, in the example you posted, you have spaces eitherside of the braces - meaning your first file would be A(space)Sample(space)(space)File.txt (which presumably does not exist). You most likely want one of the escaped spaces inside the braces so it's only added when the prefix is.
 
1 members found this post helpful.
Old 12-27-2019, 11:56 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,598

Rep: Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116Reputation: 5116
this is definitely not the way, but if you wish:
Code:
cp /tmp/{"A Sample File.txt","A Sample TEST File.txt"}
 
Old 12-27-2019, 12:15 PM   #5
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 307

Original Poster
Rep: Reputation: 52
Quote:
Originally Posted by boughtonp View Post
The reason why what you tried is erroring is because brace expansion doesn't occur inside single-quoted strings, meaning you're only passing a single argument (which is why the messages complains about a lack of destination).

Remove the quotes and backslash escape the spaces and you will get the behaviour you're after -- however, in the example you posted, you have spaces eitherside of the braces - meaning your first file would be A(space)Sample(space)(space)File.txt (which presumably does not exist). You most likely want one of the escaped spaces inside the braces so it's only added when the prefix is.
Worked, Thanks.

Code:
$ cp A\ Sample{,\ TEST}\ File.txt
Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 07:36 AM
How to find a specific word between two lines having same another word.. say_hi_ravi Programming 4 04-18-2013 07:37 PM
to extract all the part of the filename before a particular word in the filename aealexanderraj Programming 1 08-27-2012 12:08 PM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 11:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:58 AM.

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