stottj / Command-Line

0 stars 0 forks source link

Utilize wildcards in commands #11

Closed stottj closed 1 year ago

stottj commented 1 year ago

Ticket: Utilize Wildcards in Commands


Summary

Learn how to utilize wildcards (*, ?, [...]) in command-line commands for more flexible and efficient operations in a Unix-like operating system.


Description


Learning Tasks

  1. Introduction to Wildcards:

    • Learn about the basic types of wildcards and their functionality (*, ?, [...]).
  2. Using Wildcards in Commands:

    • Understand how to use wildcards with common commands like ls, cp, mv, and rm.
  3. Best Practices:

    • Discover scenarios where using wildcards can save time and effort.
  4. Pitfalls to Avoid:

    • Learn the risks of using wildcards carelessly and how to mitigate them.
  5. Hands-on Practice:

    • Exercise 1: Use the * wildcard to list all .txt files in a directory.
    • Exercise 2: Utilize ? to match single characters in file names.
    • Exercise 3: Employ [...] to list files that match specific character ranges.
    • Exercise 4: Combine wildcards to perform complex file operations.
  6. Troubleshooting:

    • Understand common issues one might encounter when using wildcards and how to resolve them.

Learning Goals


Priority

stottj commented 1 year ago

The is a wildcard which is designed to to take all files that end with whatever the was attached to. For example *.lua would mean take any file, no matter the name, that ended with .lua and perform an action with them.

The ? can represent any single character, a-z and 0-9. So if you did hd? it would like for hda, hdb, etc.

The [] specifies a range, so if you did d[a,i,u]d it would look for dad, did, dud. If you wanted range you would use the [-] reflecting start and stopping ranges for example d[a-d]d would would result in looking for dad, dbd, dcd, ddd.

The [!] is a logical NOT, it will match any character as long as it is not listed between the [ ]. For example rm filename[!9] will remove all files that do not have a 9 anywhere in the name (filename8, filename85, filename1 would all be removed).

the \ is used as an escape character to protect subsequent special character for example \ searches for a backslash.

One most becareful on how to use wildcards. If one was to use rm "*.txt" in a directory all text files would be removed. But maybe you have a lot of different type of files and would only want to see txt files. ls "*.txt" would be help weed out everything else. You could also be like me where you make your next save the first unused number. What if you wanted to move all your previous saves, which we'll say is 5, but didnt want to delete them? You could do a mv file[1-5].txt dir2. This would leave your latest file6 in the current location.

Ex 1 ls *.txt (will show all .txt files)

Ex 2 ls test?.txt (will show all files that have any character after test i.e. testA, testB, test1)

Ex 3 ls test[1-5].txt (will show all files that are named test1 through test5)

Ex 4 ls ?est[1-5].* (will show any file that has est with an ending of 1-5 i.e best5.whatever would show up.

Common issues are files not showing up if you use the wrong wild card or you placed the wildcard in a different area. .txt and chicken. are two major different searches. Also know that [ ] will have start/end points you put in so having generic info on what you are looking for is crucial.

tupleHunden commented 1 year ago

👍🏻