18553

How to remove end folder name from a path in Linux script?

Question:

I am trying to make a simple command in Linux similar to cd.. in DOS. What I tried is to make a script that changes directory to a path, which I have to get from pwd, by removing the last folder name.

So for a path: /home/usr/Downloads/images I want to get /home/usr/Downloads.

Answer1:

You can use the dirname command to do what you're asking for, it remove the last "part" from a file. If what you give it is a directory, you'll get the parent directory.

parent=$(dirname /your/path/here)

But doing a cd.. with a script is not possible - the cd would only affect the shell that the script is running in, not the shell that invoked the script.

So you have to use an alias or a function.

alias cd..='cd ..'

Or

cdp() { cd .. }

Recommend

  • How to get the second value in a dropdown box based on the selected one in the first dropdown box
  • ERROR: Additional information: External table is not in the expected format
  • Modifying files nested in tar archive
  • Can't access Tomcat 7 Manager app when running from Eclipse
  • Portable JRE on Linux - possible?
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • Write output of for loop to multiple files
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • SonarQube: Cannot deactivate rule with missing quality profile
  • Remove changes from one element when event occurs on another element?
  • Web.config system.webserver errors
  • cygwin cannot exec 'git-add--interactive' permission denied
  • Unable to install Git-core+svn by MacPorts
  • error importing numpy
  • Django simple Captcha “No module named fields” error
  • Remove final comma from string in vb.net
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Could not find rake using whenever rails
  • Android full screen on only one activity?
  • Checking free space on FTP server
  • Change Inet root folder for iis 7
  • Paperclip, set path outside of rails root folder
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • Accessing IRQ description array within a module and displaying action names
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Updated Ionic CLI but shows previous version (Windows)
  • Symfony2: How to get request parameter
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • coudnt use logback because of log4j
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Append folder name and increment by 1 using batch script
  • git trying to push non-existent file … after clearing cache