Hi there again,

I'm still a newby in Python, so I'm taking the Google Python Class.

They talk about ls -l and rm -rf (with the last command the class started laughing)
But I don't understand it.

I googled it and a hint came to me, that is is a regular expression.
But I can't seem to find the true meaning of these RE's.

Can someone please explain what they do and maybe give me a website where I can find more of these expressions?

Thanks again!

Dani AI

Generated

Nice catch, . Building on ’s answer: these are shell commands, not regular expressions. A quick way to tell the difference is that the shell expands globs like *.py and ? before the command runs, while regex (used by tools like grep, sed, awk, or Python’s re module) uses tokens like ^, $, ., and + and is interpreted by the tool, not the shell. That’s why ls will list files matching *.py, but it will not understand a regex like .*\.py$ unless another tool filters its output. For future reference, the long listing you see after ls -l shows, in order: permissions and file type, hard-link count, owner, group, size (bytes), modification time, and name. If you ever need sizes in KB/MB/GB, add -h; to include dotfiles, add -a.

Since you are on Windows, you can ignore those exact commands, but here are close equivalents without installing Linux tools:

Command Prompt (cmd.exe):

dir
del /s /q path\to\files\*.py
rmdir /s /q path\to\folder

PowerShell (recommended):

Get-ChildItem -Force
Remove-Item path\to\folder -Recurse -Force -WhatIf

Tip: keep -WhatIf until you are certain the target is correct.

Safer habits that prevent disasters (even on Linux/macOS): preview first, then delete. For example, list exactly what would be affected and confirm the path:

ls -ld -- /absolute/path && ls -la -- /absolute/path
# when sure:
rm -rI -- /absolute/path

In Python, avoid shell deletes entirely and use the standard library:

from pathlib import Path
for p in Path('.').iterdir():
    info = p.stat()
    print(info.st_mode, info.st_size, p.name)

import shutil
shutil.rmtree('some_dir')  # recursive delete of a directory tree

This is cross-platform and keeps you out of trouble with shell wildcards.

Recommended Answers

All 2 Replies

ls and rm are a couple of command-line/system programs in Unix and Linux operating systems, they aren't regular expressions.
ls is used to list the files/directories in the current directory and rm is used to remove/delete files.
As stated, ls -l lists the files/folders in the current directory. The l parameter makes ls list more detailed information about the items in the current directory.
Similarly rm -rf will delete files/directories. The r parameter makes it delete files recursively (so it will go into any subdirectories and delete files there too), the f parameter suppresses any confirmation messages (e.g. "Are you sure you want to delete this file? (y/n)") and forcibly deletes any files.
So using rm -rf will delete everything in the current directory and everything in any sub-directories.

Opening a terminal session as root and using the rm -rf command at the root of the filesystem is one sure-fire way of deleting everything on your Unix/Linux system. Which may be why some of your class laughed when they saw it. It's OK to use rm -rf if you want to remove a lot of files/folders, but you need to be very careful when doing so! (Again, just don't use it at the root of the filesystem or in any system directories when you have root priveleges!)
Even as an ordinary user, using rm -rf in the wrong place could delete all files that you have the appropriate permissions for (so all of your personal files, settings etc.).

Thanks JasonHippy!
The example was using Linix, as I understand now.
I'm using Windows as OS, so I can ignore ls -land rm -rf commands for now.

Thanks again for the quick and extensive respons!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.