Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Insurance For My Grandma-Financial Security For Seniors

    August 1, 2022

    How Much Is Insurance for a 16-Year-Old? Is It Worth It?

    June 27, 2022

    What Is Auto Insurance? How Can You Get It?

    June 27, 2022
    Facebook Twitter Instagram
    HackingMonk
    • Home
    • Boxing
    • Movies
    • Business
    • Lifestyle
    • Insurance
    • Short Term Car Insurance
    • Term life insurance no medical exam
    Facebook Twitter Instagram
    HackingMonk
    You are at:Home»How To»How to Rename a Directory on Linux
    How To

    How to Rename a Directory on Linux

    adm121nimBy adm121nimJune 13, 2022No Comments7 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Share
    Facebook Twitter Pinterest WhatsApp Email

    [ad_1]

    Linux laptop showing a bash prompt
    fatmawati achmad zaenuri/Shutterstock.com

    Renaming a listing in Linux is simple, and there are many methods to go about it. From renaming a single listing to discovering and renaming many, right here’s methods to do it.

    Your Knowledge Is Protected

    Renaming directories is one thing all of us must do on occasion.

    We would create a listing and misspell its title, and we need to put it proper. Usually, the aim of a listing adjustments over time or by the lifetime of a undertaking, and also you need to alter the title to replicate its new use. Maybe you’ve decompressed an archive file and it’s created a listing tree with the listing names in uppercase and also you’d like them in lowercase.

    Regardless of the motive. renaming a listing doesn’t do something to the information held inside it. It adjustments the path to that information, however the files and directories inside your renamed listing aren’t touched.

    Don’t rename system directories. Altering the trail to system recordsdata and instructions goes to have a detrimental impact on the operating of your laptop, to say the least. If it’s worthwhile to use sudo to rename a listing—except you actually know what you’re doing—the probabilities are you shouldn’t be renaming it.

    Utilizing the mv Command

    In essentially the most simple instances, all we actually want is the mv command. That is an integral a part of each Linux distribution, so there may be nothing to put in.

    The mv command is over 50 years outdated on the time of writing. It hails from the daybreak of Unix, when quick and cryptic instructions had been in vogue, most likely to cut back the variety of characters that needed to cross alongside gradual serial traces from teletypes and dumb terminals to the precise laptop.

    It truly stands for “transfer”, and it may be used to maneuver recordsdata from listing to listing. When you transfer a file to the identical location that it’s already in and provides it a brand new title, you’ve renamed the file. And we are able to do the identical with directories.

    There are two subdirectories on this listing.

    ls

    Listing two subdirectories

    To rename a listing we use the mv command. We have to present the present title of the listing and the brand new title.

    mv old-work archive-2

    Renaming a directory with mv

    If the listing you need to rename just isn’t in your present listing, present the trail in addition to the listing title.

    mv ~/htg/old-work ~/htg/archive-2
    ls

    Renaming a directory in a different directory with mv by specifying the path on the command line

    Utilizing the File Browser

    File browsers are capable of rename directories. The keystroke within the GNOME Information software is F2. Highlighting a listing and tapping the F2 key opens the “Rename Folder” dialog.

    Using the fie browser to rename a directory

    Kind within the new title, and click on the inexperienced “Rename” button.

    Providing the new directory name in the file browser

    The listing is renamed for you.

    The renamed directory in the file browser window

    It’s so simple as that.

    The rename Command

    In case your wants are extra sophisticated than the easy renaming of a listing you would possibly want to make use of the rename command. This lets you use Perl expressions to rename recordsdata and directories. It supplies an altogether extra highly effective and versatile method to rename directories.

    We’re going to be speaking in regards to the Perl-based rename command. There’s one other, older command known as rename which is a part of the Linux core utilities. You’ll most likely want to put in the Perl rename command we need to use.

    To keep away from title clashes with the prevailing rename command, the Perl rename command known as prename on Fedora, and perl-rename on Manjaro. On Ubuntu, the rename and prename instructions are each symbolic hyperlinks that resolve to a binary known as file-rename.

    So, on Manjaro the command you’ll want to make use of perl-rename, and on Fedora it’s prename . On Ubuntu, you should utilize rename or prename.

    To put in Perl rename, on Ubuntu it’s worthwhile to sort:

    sudo apt set up rename

    Installing rename on Ubuntu

    On Fedora, the command is:

    sudo dnf set up prename

    Installing prename on Fedora

    On Manjaro the bundle known as perl-rename.

    sudo pacman -Sy perl-rename

    Installing perl-rename on Manjaro

    Ensure you use the suitable command to your distribution if you wish to work by the examples.

    First Steps With rename

    The rename command takes Perl common expressions and applies them to a file or listing, or group of recordsdata or directories.

    In our listing, we’ve got a group of different directories.

    ls

    A collection of directories in a mixture of uppercase, lowercase, and mixed case

    Their names are a mix of lowercase, uppercase, and combined case. We will convert all of them to lowercase with an acceptable expression.

    rename 'y/A-Z/a-z/' *
    ls

    Converting directories to lowercase names

    All of the directories are actually in lowercase, whether or not they had been wholly uppercase beforehand, or contained the odd uppercase letter.

    All of the magic is contained within the expression. The expression is wrapped in single quotes “'“. That is what the complete command means.

    • y: This implies seek for any character within the first vary of characters, and substitute it for the corresponding character from the second vary of characters.
    • /A-Z/a-z/: The primary vary is all of the letters from “A” to “Z”, and the second vary is all of the characters from “a” to “z.”
    • *: The asterisk wildcard means apply this to all directories.

    In different phrases, the command reads as “for all directories, swap any uppercase letters for the equal lowercase letter.”

    Clearly, you’ll be able to rename a single listing with rename, though it does smack of overkill. You’ll be faster utilizing mv.

    rename 's/gamma/epsilon-2/' *
    ls

    renaming a single directory with rename

    The “s” on this expression means substitute. It checks every listing to see if its title is “gamma”. Whether it is, it replaces it with “epsilon-2.” Bear in mind although, that this could even have matched a listing known as “gamma-zeta”, for instance, renaming it to “epsilon-2-zeta.”

    We will keep away from this by including the beginning of string “^” and finish of string “$” metacharacters to the primary clause of the expression.

    ls
    rename 's/^gamma$/epsilon-2/' *
    ls

    Limiting a renaming action to entire directory names only

    This leaves the listing “epsilon-2” untouched.

    Utilizing rename With Different Instructions

    We will use different instructions to find the directories we would like rename to work on. If we’ve got a set of nested directories and we need to rename any that finish in “-old” in order that they finish in “-archive”, we are able to obtain that by utilizing discover and xargs.

    We have to use xargs as a result of rename doesn’t settle for piped input. The xargs command overcomes that downside by accepting the piped enter and including to the command line of one other command as a command line parameter.

    Our command appears like this:

    discover . -depth -type d -name "*-old" | xargs -r rename "s/outdated$/archive/"
    • .: We inform discover to begin looking within the present listing. This might be any path, after all.
    • -depth: Use a depth-first search. This implies the contents of deeper nested subdirectories are processed earlier than increased ones.
    • -type d: Seek for directories, not recordsdata.
    • -name “*-old”: The search clue. We’re on the lookout for directories with names ending in “-old.”
    • |: We’re piping the output from discover into the xargs command.
    • xargs -r: The -r (no run if empty) means don’t run the command if there are not any matching directories.
    • rename “s/outdated$/archive/”: The rename command to be run.

    Our listing tree appears like this earlier than the command.

    The directory tree before our renaming command

    We run our command:

    Our renaming command using find, xargs, and rename

    And we are able to see that the entire matching directories together with the nested ones have been renamed.

    The directory tree after the renaming command

    Horses for Programs

    Renaming a listing doesn’t want something greater than mv. When you want GUI purposes you should utilize your file browser. When you’ve acquired numerous directories to rename, and particularly in the event that they’re scattered all through a listing tree, you’re going to wish the flexibleness of rename.

    RELATED: How to Manage Files from the Linux Terminal: 11 Commands You Need to Know



    [ad_2]

    Source link

    adm121nim
    • Website

    Related Posts

    NASA documentary celebrates Black space explorers | Digital Trends

    By adm121nimJune 20, 2022

    Beer maker transforms brewing waste into denim jeans | Digital Trends

    By adm121nimJune 20, 2022

    Is he…you know?: Literature’s best queer-coded characters | Digital Trends

    By adm121nimJune 20, 2022

    How to Apply for Netflix’s Squid Game: The Challenge

    By adm121nimJune 19, 2022
    Add A Comment

    Leave A Reply Cancel Reply

    Don't Miss

    Insurance For My Grandma-Financial Security For Seniors

    By adm121nimAugust 1, 2022

    Life insurance can be a great way to provide security for your grandmother. Life insurance…

    How Much Is Insurance for a 16-Year-Old? Is It Worth It?

    June 27, 2022

    What Is Auto Insurance? How Can You Get It?

    June 27, 2022

    How Long Does It Take To Get Renters Insurance?

    June 27, 2022
    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Our Picks

    Insurance For My Grandma-Financial Security For Seniors

    By adm121nimAugust 1, 2022

    How Much Is Insurance for a 16-Year-Old? Is It Worth It?

    By adm121nimJune 27, 2022

    What Is Auto Insurance? How Can You Get It?

    By adm121nimJune 27, 2022

    Subscribe to Updates

    Get the latest creative news from SmartMag about art & design.

    About Us
    About Us

    We are a news website that covers the latest in current affairs, politics, business, sports, and entertainment. We strive to provide our readers with an unbiased and well-rounded view of the world around them, and we believe in giving a voice to the voiceless.

    Our Picks

    Insurance For My Grandma-Financial Security For Seniors

    August 1, 2022

    How Much Is Insurance for a 16-Year-Old? Is It Worth It?

    June 27, 2022

    What Is Auto Insurance? How Can You Get It?

    June 27, 2022

    Subscribe to Updates

    Get the latest news from HackingMonk about movies, music and celebrities.

    Facebook Twitter Instagram Pinterest TikTok
    • Home
    • About Us
    • Privacy Policy
    • Contact
    © 2023 HackingMonk. Made WIth ❤️ By Shine Barbhuiya

    Type above and press Enter to search. Press Esc to cancel.