LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-17-2020, 08:56 PM   #1
Alrick2004
LQ Newbie
 
Registered: Jan 2020
Posts: 4

Rep: Reputation: Disabled
How do I issue this linux command?


Assume that your current directory contains the files 'labtest', 'labtest1', 'labtest2', 'labtest2a', 'labtest3', and 'labtest4'.

How do I issue a command to delete only the files 'labtest1' and 'labtest2' using one ambiguous pathname. (character class)
 
Old 01-17-2020, 08:58 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Sounds like homework. See the LQ rules about that.
 
2 members found this post helpful.
Old 01-17-2020, 09:08 PM   #3
Alrick2004
LQ Newbie
 
Registered: Jan 2020
Posts: 4

Original Poster
Rep: Reputation: Disabled
Need Help

practicing for school. But need help with this one.
 
Old 01-17-2020, 09:12 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Welcome to LQ Alrick2004,

Advice from our rules/guidelines:
Quote:
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
 
Old 01-17-2020, 09:35 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I'd use brace expansion.

Some explainers:

https://www.linuxjournal.com/content...race-expansion
https://www.gnu.org/savannah-checkou...race-Expansion
https://www.linux.com/tutorials/all-...y-braces-bash/
 
2 members found this post helpful.
Old 01-17-2020, 09:55 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Alrick2004 View Post
Assume that your current directory contains the files 'labtest', 'labtest1', 'labtest2', 'labtest2a', 'labtest3', and 'labtest4'.

How do I issue a command to delete only the files 'labtest1' and 'labtest2' using one ambiguous pathname. (character class)
You use the rm command with an "ambiguous pathname" (a term I have never heard but which makes sense to me). The ambiguous pathname is the name of a file where some characters are expressed as character classes.

I suggest you review the concepts of filename expansion and character class from your course. These are fundamental concepts in the shell. You may also use the Bash resources in my signature.

For example, [1-4] means a character in the range of 1 to 4. labtest[1-4] means files that are named labtest1, labtest2, labtest3 or labtest4. You could call it an ambiguous pathname

Rather than a range like [1-4], you can write a character class like this: [acfgh] or [12], which means a single character out of the set (a c g g h) or the set (1 2).
 
2 members found this post helpful.
Old 01-18-2020, 08:39 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
build yourself a test bed using the examples as files by the same names, then do yourself a bash script, for easy quick editing, or command line it, changing the search patterns until your get the results you need. you can use echo in place of delete (rm) to see what you are picking out of the bunch of the files by similar names.

using a script, you can run it in the same dir as the files or give it the path to the dir to search in and run the script elsewhere.

Code:
#!/bin/bash
searchPath=
you can set the script to give you the output of what it is doing
Code:
set -xv
you can set this particular script to create the files by name for your learning purposes. this is set to have the script be ran in the same dir as the files are created in to look for the matching two filenames
Code:
#creates files by name to be tested against.
touch tabtest labtest1 labtest2 labtest2a labtest3 labtest4
you can and there are other ways to skin this cat but this is just one suggestion. put the names in an array then use that array to check against the filenames being read into the script.
Code:
#file names to be found put in an array.
patteren1=( labtest1 labtest2 )
using nested loops, (or two loops, one in a function) one to read in the files in question and another loop to check all of the files against what is in the array with the filenames in question one is looking for. using a function, because they are useful.
Code:
IsItTheOne?()
{
#using string manipulations to find matching patterns

for (( i = 0; $i < ${#patteren1[@]}; i++ ))
do  
   if [[ ${g##*/} = "${patteren1[$i]}" ]] ; then
	echo "found it $g"
   fi
done
}
Code:
for g in $(pwd)/* ; do
    IsItTheOne? $g
done
if you were to put that together in working order you might find a glitch (Bug) that needs to be worked out.

this is just to demonstrate there is more than one way to get the same results in scripting and programming.
knowing loops and string manipulations.
strings and bash

Last edited by BW-userx; 01-18-2020 at 09:15 AM.
 
Old 01-18-2020, 06:43 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Uh, don't listen to BW-userx. All you needed was one of the following two patterns (that's the word you were looking for):
Code:
rm labtest{1,2}
Code:
rm labtest[1-2]

Last edited by dugan; 01-18-2020 at 06:44 PM.
 
2 members found this post helpful.
Old 01-18-2020, 07:05 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by dugan View Post
Uh, don't listen to BW-userx. All you needed was one of the following two patterns (that's the word you were looking for):
Code:
rm labtest{1,2}
Code:
rm labtest[1-2]
mine works your's prob works, and I bet there is still other ways to do this...
Quote:
this is just to demonstrate there is more than one way to get the same results in scripting and programming.
all you're doing is denying a truth, and telling someone else to do the same.

Last edited by BW-userx; 01-18-2020 at 07:41 PM.
 
  


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
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
Command Substitution: $(command) vs `command` penguinbody Linux - Newbie 9 04-15-2016 07:00 AM
Wrote Shell in C - piping output from first command to second command, sending to output file issue KarenWest Programming 2 03-31-2016 05:27 PM
Fedora 13 Installation issue -- Unable to install multiple linux distributions ---partition issue navneethr Linux - General 6 12-22-2010 11:17 PM
Postfix issue ; Must issue a STARTTLS command first after forcing TLS sarajevo Linux - Server 3 05-14-2009 08:28 AM

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

All times are GMT -5. The time now is 05:46 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