Ravindra BagaleCourses & study guides मराठी Track your progress

Chapter 3: Linux Basic Commands

AWS Cloud

3.3 Working with files and directories

Friends, now let's create, copy, move and delete files and folders. For the Amul-style dairy website we are going to create a fresh practice folder in your home directory – run the commands below in order.

Command Purpose Example
mkdir Make directory mkdir project, mkdir -p a/b/c (create parents)
touch Create a missing file or update timestamps touch index.html
cp Copy cp a.txt b.txt, cp -R -- site backup-copy
mv Move or rename mv -i -- old.txt new.txt, mv -i -- file.txt archive/
rm Remove file rm -i -- test-note.txt (inspect the exact lab file first)
rm -r Remove directory recursively rm -r olddir
rmdir Remove empty directory rmdir emptydir
ln -s Create symbolic link (shortcut) ln -s -- drafts/index.html lab-shortcut.html
file Show file type file /bin/ls
stat Detailed file info stat index.html
practice_site=$(mktemp -d "$HOME/practice-site.XXXXXXXX") &&
  cd -- "$practice_site" &&
  mkdir -- css &&
  touch -- index.html css/style.css &&
  cp -i -- index.html index.bak &&
  mv -i -- index.bak old-index.html &&
  ls -R -- . &&
  rm -i -- old-index.html

rm is permanent

There is no Recycle Bin on a Linux server. The shell will not send files to a trash folder when you run rm. For the lab, work inside a fresh practice directory and inspect the exact path and contents first. Use rm -i -- sample.txt for one known file or rmdir -- empty-folder for an empty directory. Keep a checked backup before removing a directory you might need.

Ravindra Bagale's Tip

Many students run rm -rf without reading the path even once – and there is no Recycle Bin on a server. Read the command twice before pressing Enter, and if in doubt, first look at the same path with ls or use rm -i. Pay attention!

Lab

The Amul-style dairy sathi mkdir -p ~/labs/mauli/{html,css,images} ne teen folders ekach command ne banva, html madhe index.html banva, tyachi copy index.bak kara, ti rename kara aani shevti ls -R ~/labs/mauli ne structure check kara.

Prepare a release without losing the previous page

Mauli Dairy has given you a sample delivery page and a note that says the opening time needs to change. The person who looks after the site asks you to prepare a new copy and keep the previous version available. You can do this with ordinary Linux file operations, even if you have never edited a server. In this lesson you will set up the folders, create small test files, read what the commands did, and explain which copy you would send for review.

Start by naming the result you want. Do you want an empty destination ready for work, a new file containing the same bytes, a different name for one existing file, or an old test entry removed? A Linux command acts on particular paths and does exactly the operation you requested when it can. If you confuse copying and moving, the command can succeed while the result is wrong for the team. Write the before and after names on paper before choosing the command.

We will work with sample content in a newly named lab folder. The actions do not require administrator permissions, and no exercise path is a real web root. You should be able to rerun the setup in a new folder to compare results without replacing an earlier exercise. On the practice machine, confirm your host and account just like you did in the navigation lesson. The folder you create here is the extent of every write, link and removal in this walkthrough.

Set up separate places for source, draft and released copies

Draw a folder with four children named incoming, drafts, releases and history. Incoming holds the material the dairy sent. Drafts holds your changes. Releases holds the candidate you verified. History holds the sample previous version you can compare or restore. These roles matter even when the folders happen to hold files with the same name. The folder history here is just another directory that you manage, not an automatic record kept by the shell.

release_lab=$(mktemp -d "$HOME/release-files.XXXXXXXX") &&
  cd -- "$release_lab" &&
  mkdir -- incoming drafts releases history &&
  printf 'Release exercise: %s\n' "$release_lab"

The first command asks for a uniquely named directory and saves the result in a variable. Read the printed path after setup and compare it with your own home location. If the sequence stops early, check its message before entering more commands. The double ampersand joins steps with a success condition. In this exercise it prevents the creation of folders if entering the new workspace failed. Remember that the variable is defined for this terminal session. Write down the printed path if you plan to continue in another shell.

Use mkdir when the result should be a directory. Each name after the options can request a separate directory, which is why one command can create all four children. The -p option lets you ask for a nested path such as drafts/css, creating any missing parent components. It can also treat an already existing directory as an acceptable result. If the name is occupied by a regular file, it cannot work as a parent directory. Inspect the resulting names and types if the setup does not look as expected.

Run a long listing for the four entries and look for the leading d. A file request uses names beneath those directories, while a directory creation adds another branch. Interrupt the lesson for a small prediction: if you type mkdir -p drafts/assets/images in the lab root, which new entries would you see at each level? Use one listing of drafts and one of drafts/assets to confirm. This shows what it means for a path to have multiple missing components before the operation can create its final directory.

Choose between creating an empty entry and writing content

touch is useful for creating an empty file when the path does not exist. On an existing file it normally changes timestamps rather than removing its contents. That makes it different from redirecting new output into a file. Use touch when you need a placeholder or an intended timestamp change. Write the data with an editor or a suitable command when you need real contents. A listing can show that the placeholder is present, and a byte count tells you whether it is still empty.

cd -- "$release_lab" &&
  touch -- 'incoming/notes.txt' &&
  wc -c -- 'incoming/notes.txt' &&
  printf '%s\n' 'Dispatch desk' 'Opens at 6' > 'incoming/delivery page.txt' &&
  touch -- 'incoming/delivery page.txt' &&
  cat -- 'incoming/delivery page.txt'

The notes entry starts as a zero-byte file in the fresh lab. After the content write, the delivery page has two lines. The second touch leaves those lines in place. Verify that using the displayed cat output. When we ask for a filename with spaces, the quotes keep it together. More than one path can be passed to some commands, but here each command is meant to name exactly one chosen file. Your shell strips the outer quotes and gives the whole name as its argument.

The greater-than operator tells Bash to open a destination and redirect standard output there. If a regular file is already present, the ordinary form can truncate it before the command writes anything. Appending with the double operator puts new data at the end instead. This is why the output destination deserves its own check even when the command you run looks harmless. If a command reads a file, choose a different write path while preparing a new version. That makes it possible to verify the result and keep the previous version.

Try a separate file with two simple appends, such as a review decision and a time. Check that you have two lines, then add a third using the same append operator. Write down how your prediction differs from what would happen with a normal overwrite. Next explain which behavior you want for a new report and which you want for an ongoing record of events. This practical decision tells you more than memorising that one symbol means write and two symbols mean append.

Sometimes the same file appears unchanged after you edit a draft. Start by checking which path your command actually used and whether it wrote to that path. The file size and modification time provide context, and reading the expected text provides more direct evidence. A timestamp can also change without a content change, as you just saw with touch. “It changed at this time” and “the intended line is present” are different observations. Use whichever proves the result you need and keep both if they answer different questions.

Make a copy you can compare with its source

Read the copy command as source first, destination second. A copy should normally leave the source in place and create or update the destination. If the destination you name is an existing directory, the tool can place the source inside that directory using its basename. If the destination is a file path or names a new file, the result goes at that exact name. Before a copy, identify which of those situations you intend. A missing destination directory and a new filename are not interchangeable.

cd -- "$release_lab" &&
  cp -i -- 'incoming/delivery page.txt' 'drafts/delivery page.txt' &&
  ls -l -- 'incoming/delivery page.txt' 'drafts/delivery page.txt' &&
  cmp -- 'incoming/delivery page.txt' 'drafts/delivery page.txt'

The two long-listing rows confirm each path exists in its own branch. A successful cmp with no output confirms their bytes match right now. The -i flag asks before overwriting an existing destination. In a newly created workspace there should be no earlier draft, so you normally will not see a prompt. If you do, read the path on the prompt and decide whether the existing learner's draft should be protected. For this exercise, keeping both drafts by choosing a new name is a straightforward way to preserve earlier work.

Metadata is information about a file, such as its owner, mode and timestamps. A regular copy and a verified byte match do not automatically prove that ownership, permissions or other metadata stayed the same. Your task decides whether those properties matter. For a teaching handout, exact text might be sufficient. An operational backup can also need access permissions, links and hidden files. In a later backup lab, choose a method that preserves the properties you need and test a restore. The copy here is for preparing one editable document.

Use cp -R if the selected task is to copy an entire directory hierarchy. Decide whether the expected result includes the source directory name as a child or copies its contents directly into a new target. The rules depend on which source forms you use and the current state of the destination. In a fresh lab, copying a source directory to a destination name that does not exist creates a separate tree under that name. A copy into an existing destination directory can place the source directory beneath it. Verify the result with a shallow listing before doing anything with the copied files.

For a small comparison, after confirming that the name history/incoming-copy does not exist, copy the incoming directory there. Check that the new tree contains the notes file and delivery page. Read one copied file and compare it to the source. If you instead find the page one layer deeper than your notes show, inspect which directory already existed at the destination before the command. In a real release this can be the difference between a file under the configured web root and another folder the web server will not serve.

A shell wildcard can expand before the copy program sees its operands. Suppose your folder holds several text files and your copy command uses an unquoted asterisk as part of a pattern. The shell finds matches and passes the resulting path list. A broad pattern can include more items than you planned. Preview them with a read-only listing or a small explicit argument display. In the beginner exercises, use the known names where you can. When you need a pattern, record how many files you expected and what the matched names actually are before giving them to a write command.

Rename a draft or move it into an agreed folder

Use mv when the source entry should end up at a different path, with its former name no longer needed. A move within one filesystem can be a rename of a directory entry. A move to a different filesystem can be implemented by copying and then removing the old entry if the copy succeeded. This matters when you estimate work or consider what might remain if a move is interrupted. In the lab, both paths are under the same scratch folder, so you can focus on the intended visible result.

Prepare a second draft with a new delivery time. Keep the incoming file unchanged. Start from the copy you verified and decide which name will signal that the version is ready to review. One approach is to append a review note to a separate test file. Another is to write a properly revised two-line page as a new draft. For this release we will create the revised page and rename it to make its status easier for the next student to recognise.

cd -- "$release_lab" &&
  printf '%s\n' 'Dispatch desk' 'Opens at 7' > 'drafts/delivery proposed.txt' &&
  mv -i -- 'drafts/delivery proposed.txt' 'drafts/delivery reviewed.txt' &&
  ls -l -- drafts &&
  tail -n 1 -- 'drafts/delivery reviewed.txt'

The drafts listing should include the copied morning input and the revised name. The proposed name should be absent after a successful rename to the new name. Read the tail output to see “Opens at 7.” An overwrite prompt means the destination already exists, so read the question instead of assuming the move happened. Before using the next command, compare the source and destination state with what you wanted. This checks both the change of name and the contents of the object you selected.

It is important to read the word “move” the same way when you change folders. If you move the only copy of the reviewed page into a release folder, the draft location will no longer have that entry. That can interfere with a review process that expects to compare both folders. We can use a copy for our final publication so the two versions remain available. Decide by asking whether you need an identical separate entry or a transfer of the current entry. Then test the required presence or absence of the source as well as the destination.

If a move leaves you unsure where the file went, look at the exact destination operand and the directory state when you ran it. A directory at that destination can cause a basename to appear below it. A destination file can have been replaced when the command was permitted to do so. Use small read-only listings of relevant paths and compare their types. If a conflict happened, keep the observed files while diagnosing it. Make a second change only after you can explain which object is which.

Check what kind of object you are about to handle

A name ending in .txt is a convention. The real contents decide whether the file is ordinary text or something else. Use the file utility for a description of what it recognises in a sample when it is installed. It can identify familiar formats from data patterns, and its result is an interpretation rather than a guarantee that the program using the file will accept it. A path with the right extension can still hold an empty file, an error page or content in a different format.

stat reports detailed information associated with a path. The display depends on the operating system and options. Find the size, file type, access details and relevant timestamps before you draw conclusions. On GNU/Linux you can ask GNU stat for specific display fields. The examples below use the tool's ordinary output so a beginner can see several fields together. Look first at which path the output describes. Different commands can choose whether to report a link entry or its target.

cd -- "$release_lab" &&
  stat -- 'incoming/delivery page.txt' 'drafts/delivery reviewed.txt' &&
  wc -c -- 'incoming/delivery page.txt' 'drafts/delivery reviewed.txt'

Both files are regular text fixtures and can have the same number of bytes even though the opening hour differs. Comparing the byte count alone will miss the change from six to seven. Read the last line of both files and use a text comparison when you want to see the change. This gives you three different observations. Size describes how many bytes there are, the content read describes what characters are there, and timestamps describe events reported by the filesystem. None of those values should be mistaken for a different one.

In a text comparison, the first input is the baseline and the second is the candidate. GNU diff can report a successful comparison that found differences using a nonzero result. Treat the exit status appropriately instead of announcing that every nonzero result means the comparison failed to open a file. The human-readable difference shows which lines belong to each version. In the release exercise, you expect the old opening hour to appear on a removed line and the new hour to appear on an added line.

Choose an inspection before using a destructive command. A regular file can be read with a small text preview if you know it is a fixture. A directory should be listed rather than read as a regular file. A link can be examined as its own entry. In a real exercise with unfamiliar input, avoid printing large unknown files into your terminal. Inspect the type and size first and decide which read-only tool can reveal the evidence you need.

Use a shortcut and verify the route it takes

Imagine a reviewer wants the current candidate from a stable name. A symbolic link can give that name without making another copy of the file's contents. The link stores a target path, and a later operation can follow it to the target. If the target changes or disappears, a link can lead somewhere different or fail. A copy has its own separate bytes. Decide whether you want another data copy or a shortcut before using either tool.

The lab link below uses a path relative to the directory containing the link. It points from releases up to the scratch root and down into drafts. Trace that route before typing. If you type the target as though it were relative to the terminal's current directory, you may get a different result. This is another reason to avoid assuming current directory and link location are the same idea. Read both the named link and a known line from the file it resolves to.

cd -- "$release_lab" &&
  test ! -e releases/review-link.txt &&
  test ! -L releases/review-link.txt &&
  ln -s -- '../drafts/delivery reviewed.txt' releases/review-link.txt &&
  ls -ld -- releases/review-link.txt &&
  tail -n 1 -- releases/review-link.txt

The first two tests verify that the lab name is unused for both existing objects and link entries. The long listing should show a leading l and the target after its arrow. The final line should read “Opens at 7.” A read through the shortcut opens the target. Removing the shortcut itself leaves the target in place when you name the link entry directly without following it. If you later rename the target, the link still stores its old target path until you change the link or restore that path.

You might hear about a hard link in the permissions or filesystem chapter. A hard link is another directory entry for the same underlying file object. There is no separate target path to be expanded the way a symbolic link works. Removing one hard link removes that name. Another hard link can still keep the object reachable. This is why a long listing shows a link count. For this first exercise, use a symbolic shortcut only. It makes the competing ideas of another name, another path and another copy easier to see.

Ask a classmate to explain these three possible verification questions. To check the stored shortcut target, list the link itself. To check that a user reaches the intended file through it, read the known test text through the link. To check that the reviewer has an independent backup, compare two separate copies in the appropriate folders. A link that resolves correctly does not establish that a separate backup exists. The same exercise lets you answer the three questions with different evidence.

Remove a known disposable entry and check its absence

Working with files includes deliberately removing one when it has served its purpose. Begin with the entry you can identify exactly, not with a broad pattern. In this lab, the empty notes file in incoming is a disposable placeholder. The original delivery page beside it is the input you must keep. List the directory first, read the two names and select the exact notes path. If you are uncertain which path belongs to the exercise, make a new disposable placeholder and remove that instead.

rm normally removes the named directory entry, and a normal invocation does not send it to a desktop trash folder. Use confirmation where appropriate and keep a verified backup for data that may need to return. If you name a directory, ordinary removal of a file is a different operation from traversing a whole directory tree. The -r option intentionally includes contents. Learn the scope of that option before using it in real work. When a directory should be empty, rmdir is a useful explicit check because it refuses a nonempty directory.

cd -- "$release_lab" &&
  ls -l -- 'incoming/notes.txt' 'incoming/delivery page.txt' &&
  rm -i -- 'incoming/notes.txt'
ls -l -- "$release_lab/incoming"

Read the confirmation and answer only if it names the disposable notes file you intend to remove. If you say no, the notes entry should remain, and you can still read the listing. If you say yes, the listing should show the delivery page and no notes file. This is a targeted check. The first objective is not how fast you can remove several entries, but whether you can verify that a chosen entry disappeared and its neighbor remained. Online instructions sometimes show broader removal examples. Scope each action to the paths you own in the lab.

There is also a useful explanation for a name that disappears while disk use looks unchanged. A process can retain an open reference to a file after a directory entry is removed. The filesystem may keep the data while that reference remains or while another hard link names the same object. This matters in disk-space troubleshooting, especially for long-running applications with logs. In this exercise, the notes placeholder is zero bytes, and the important result is the name change. Do not promise a particular number of freed bytes from removing a name alone.

Leave old exercise data in place if you are using it to troubleshoot. The order is: inspect the exact target, decide whether it needs preservation, perform the smallest removal that solves the task, then verify the result. For example, if the published page is wrong, deleting all releases removes useful evidence and can take away the last working copy. Put the intended version in a separate review location instead. Once the review is complete, choose which copy the exercise needs to keep.

Understand the list a pattern would pass to a command

In Bash, an unquoted wildcard is expanded by the shell before tools such as cp or rm run. A pattern that looks like one thing on screen can become several operands. Try inspecting a pattern instead of using it for a write. In a newly created folder with two ordinary text fixtures and one hidden file, check which names the default pattern brings back. Then compare with a listing that includes dot names. This helps you decide whether a pattern would include all the files you intended.

cd -- "$release_lab" &&
  mkdir -- preview-pattern &&
  touch -- 'preview-pattern/one file.txt' 'preview-pattern/two.txt' 'preview-pattern/.hidden.txt' &&
  printf 'Matched: [%s]\n' preview-pattern/*.txt &&
  ls -la -- preview-pattern

In typical Bash settings the significant output is two bracketed names with the spaces intact in the first one. The hidden file appears separately in the full listing. The exact order can depend on shell and locale settings. The shell found the filenames before printf received them. Because a matched name is already a separate operand, its internal space can be part of that one operand. The quoting problem changes when you paste a textual path with an unquoted space. Keep those two steps separate.

What if no files match? In typical default Bash settings the literal pattern can remain in the command's argument list. Other settings can remove the unmatched argument or refuse to run that command. A list printed with square brackets can show which case you got. Avoid assuming that a command targeting a pattern automatically becomes harmless when there is no match. Before any file-changing exercise with a pattern, inspect the intended directory and list you expect. An exact path is easier to verify when there are only one or two targets.

Some file names begin with a hyphen. A command can interpret such an operand as an option if the parser has not been told where options end. In these exercises, -- is the explicit end for the GNU tools that support it. Another way to pass such a name safely from the current directory is to prefix it with ./. That makes the first character a dot. Learn why those forms matter before pointing a file utility at names supplied by other people.

Write a short record before attempting a patterned operation. Say which directory you will use, which names you expect and what result would show an unintended match. Try the argument preview as a separate invocation. This deliberate pause helps when an old command in history used a different workspace. Check the current folder again if you switch shells. Once you can explain the matching list, practise copying only those disposable files into a fresh test destination and verify their paths.

Publish a verified copy and practise recovering from a mistake

The final task works with two labelled versions. Preserve the earlier page in history using a distinct name, and then place a copy of the reviewed page in releases. Before copying, read the end of both sources. Their predicted values are six and seven. Choose exact destination filenames and inspect them if they already exist. In a new workspace the release name should be free. Use the previously stated after-state to verify that both source and verified release remain present and that the previous copy can still be read.

cd -- "$release_lab" &&
  cp -i -- 'incoming/delivery page.txt' 'history/delivery before.txt' &&
  cp -i -- 'drafts/delivery reviewed.txt' 'releases/delivery current.txt' &&
  cmp -- 'drafts/delivery reviewed.txt' 'releases/delivery current.txt' &&
  tail -n 1 -- 'history/delivery before.txt' 'releases/delivery current.txt'

The comparison should succeed with no data difference. The tail output labels the two files and reports the earlier time with the history name and the new time with the release name. Read those headings as well as their last lines. Verification has two parts: you can get the old value back from history, and the new path holds the reviewed value. If you use a shortcut in your own report, also verify its target and the known line a reader sees through it. A copy comparison checks files on disk, and a separate test request would check a real site's configured path.

One common mistake is to copy into an existing directory while planning for an exact pathname. In a separate scratch folder, make the target a directory with a predictable name and predict where the copy will appear. Then inspect the target and compare the content. This is a safe way to learn why a file may be nested a level under the place you expected. Once you understand that result, prepare a separate test with an explicit filename. Compare the two directory trees to see which action matches the requirement.

A second mistake is to rename a file that a symbolic link depends on. On a new test file and link, move the test file to a second name. Inspect the link entry and try a read through it. The link still exists, but the old target no longer resolves. Restore the target name or make a new link after checking the intended location. This gives you an explanation for a missing page when the displayed shortcut seems to be present. Work only on disposable link entries for this demonstration.

A third mistake is to run the next command after an earlier directory change failed. Try a short sequence in your workspace that uses a missing directory followed by a read-only observation after a double ampersand. The observation should not run. Then inspect the unchanged working directory with a separate command. For a release procedure, apply the same ordering to verification: a release copy should be checked before replacing any previously named version. Write down what would be left after each step if the next one failed.

Keep a handover note with the machine, full workspace, old and new paths and the evidence of a correct release. State whether the previous copy is usable and how you confirmed it. You can practise restoring it into a different disposable review path and compare with the saved history. This maintains a separate recovery test while preserving the prepared release. When the training finishes, leave the scratch path and remove only the file entries and directories you created, or label and keep that workspace for your next exercise.

When moving between release stages, check for the kind of destination your procedure expects. A backup written to a filename that already points to a directory can arrive inside it instead of taking the location you listed in your notes. A filename that resolves through a link can lead to a copy you did not intend to update. For the lab, name an unused, explicit destination first. Run a long listing of the parent so you can identify any unexpected entry with that same name. After the operation, inspect the exact new path and read the known message. If the path and the content do not both agree, keep your saved source and find where the copy landed before you run another write. This gives you two checkpoints where an earlier mistake can be caught while the previous version still exists.

One common review question is whether the dry run changes any state. A dry run should only show a plan. A real file copy writes data, even if you intend to remove it afterward. In a beginner exercise, echo the names and intended steps before a write or use a separate disposable destination to practise. Record which checks simply read and which operation introduced the new version. That distinction will help when a later deployment plan can alter multiple resources.

Explain file operations with their results

What is the difference between copying and moving the only draft?

I would first ask whether the original draft needs to remain in its old location. Copying generally leaves it there and makes a separate destination. Moving transfers the name and can remove it from the old location. I would read the exact source and destination and verify both afterward. Moving across filesystems can involve a copy and removal, which matters if the operation is interrupted.

How would you verify that a new file really is a copy?

I would first identify both paths, then check size and compare the contents of the source and destination. A quiet successful byte comparison is direct evidence of equal bytes at that moment. If the task also requires matching permissions or timestamps, I would check those separately. If a service should show the file, I would verify its configured path and response too.

Why can a touch command change the time without changing the page?

Touch requests timestamp changes on an existing file. It can create an empty file when that path is absent. To establish the content of the page, I read a specific known line or compare two files. The time by itself cannot prove that a requested edit landed. I would use these two observations together when reporting the version I saw.

A link shows up in a listing, yet the read fails. What does that suggest?

The directory entry for the link can remain when the stored target is missing, mistyped or inaccessible. I would read the target from the link's own listing, resolve it from the correct starting directory and check the intended target and any access errors. A listing of the link itself and a read through the link measure different things.

Which checks come before a removal?

I identify the machine, my current directory and the exact disposable target. I list the same path and decide whether a checked copy should be kept. For one file I use an appropriately scoped removal and verify that the neighboring entries remain. If the task is to remove an empty directory, I use the operation that refuses to touch a directory containing files.

Check your understanding

You need an empty placeholder at a name that is unused. Choose between touch on the new path, a move of the only input file, and a recursive copy of the entire workspace.

Answer and reasoning

Use touch on the new path. It can create the empty file. Before doing that, confirm the destination name is not an existing file whose timestamps should remain as they are.

A checked original and draft contain different opening hours but have the same byte count. Choose the next check: compare the contents, conclude they are identical, or remove one since the sizes match.

Answer and reasoning

Compare the contents. A single character can change while the number of bytes stays the same. A small text diff or a read of the known line answers which hour it contains.

You want the reviewed draft to remain available and a second path to contain the same bytes. Choose copy, move, or remove for the first publication step.

Answer and reasoning

Copy to the checked destination and compare afterward. Moving would remove the earlier name from the draft directory in a successful transfer.

A lab destination should now be empty. Which tool refuses to remove it if a file remains: rmdir, rm -r or touch?

Answer and reasoning

Use rmdir. Its empty-directory requirement checks the constraint you care about. A recursive removal is a different operation and visits the contents.

A shell pattern expands to more paths than you predicted. What is the next safe action: inspect the list and narrow your selection, run the original write, or add a force option before inspecting?

Answer and reasoning

Inspect the list and narrow your selection. The shell already showed that your pattern has broader scope than intended. Use exact disposable paths for the practice operation and compare the result with your plan.

Revision cards

Create carefully

Use mkdir for a directory. Touch can create an empty file or change timestamps of an existing one. Inspect type and contents afterward.

Copy or transfer

A copy keeps a source and destination. A move changes the location or name. Choose based on which entries should exist afterward.

Test more than size

Use the byte count for length, a comparison for exact bytes and a file read for the expected message. Permissions and times are additional properties.

Link or independent copy

A symbolic link holds a target path. A read through it can fail when the target moves. Verify both the shortcut and the content it reaches.

Preview the matching names

The shell can expand a wildcard into multiple operands. Inspect the resulting list before giving it to a file-changing utility.

Remove only the verified target

Read the exact path and check which neighboring entries should remain. Use rmdir when the directory must be empty.

Official references