Linux up one level

Move files from specifically named folders / move files up one level

The goals is to move files out of the lowest level folder, back up one directory. This does a great job but moves files higher up the folder hierarchy as well.

find . -name '*.jpg' -type f -execdir mv '<>' ../ \; 

I’ve tried changing the ‘*.jpg’ parameter to include the folder and tried using the folder instead of the dot after file. Also tried -maxdepth and -mindepth but they had no effect. EDIT: This is for multiple folders i.e. it needs to recurse through an entire drive.

If all .jpg ‘s are in the same folder you can use mv -t ../ *.jpg . if they on different folders then your command will move each of them to their respective parent directories.do you want to move them to the same directory?

I don’t understand. What do you need that cd photos; mv *jpg ../ or mv /path/to/photos/*jpg /path/to won’t do? Do you have multiple directories?

So, you want to traverse whole directory tree and move each file to its respective parent directory? Is that precise ?

4 Answers 4

Move files from the lowest sub directory up one level

The script below searches a directory for the lowest sub directory (= without sub directories) recursively and moves all found files in the folder(s) up one level, as literally asked for in The goals is to move files out of the lowest level folder, back up one directory:

#!/usr/bin/env python2 import shutil import os import sys directory = sys.argv[1] for root, dirs, files in os.walk(directory): for dr in dirs: dr = root+"/"+dr # find folders without subfolders (= lowest level) if len(next(os.walk(dr))[1]) == 0: # direct superior directory up = dr[:dr.rfind("/")] # move files from lowest level one level up for f in os.listdir(dr): shutil.move(dr+"/"+f, up+"/"+f) 

To use:

python /path/to/reorganize.py
python /path/to/reorganize.py '' 

EDIT

As mentioned in a comment, with a little change, the script can be used to only move files from a folder name up one level. The moste efficient way is to do that directly after

#!/usr/bin/env python3 import shutil import os import sys directory = sys.argv[1] for root, dirs, files in os.walk(directory): for dr in dirs: if dr == "": dr = root+"/"+dr # direct superior directory up = dr[:dr.rfind("/")] # move files from lowest level one level up for f in os.listdir(dr): shutil.move(dr+"/"+f, up+"/"+f) 

Источник

Cd Up One Level Linux

So if you’re in directory A: That will move right back to directory A. expands to the previous directory you were in. To change the directory and move to another directory cd command is used in the following ways.

Go up several directories in linux

When I want to go one Directory up I use

Читайте также:  Md5 hash generator linux

But when I want to go 7 directories up is there any way to do this other than just typing seven (7) times cd .. ?

Also another useful navigation tip is if for example lets say you keep switching from a directory (call it A) to another (call it B) that’s 7 directories up, in your case.

So if you’re in directory A:

A> cd ../../../../../../../ B> // Now you're in directory B and want to go back to A B> cd - 

That will move right back to directory A. — expands to the previous directory you were in.

function cd_up() < cd $(printf "%0.0s../" $(seq 1 $1)); >alias 'cd..'='cd_up' 

UPD: Or make more powerfull variant, cd to dir name in current path:

# cd up to n dirs # using: cd.. 10 cd.. dir function cd_up() < case $1 in *[!0-9]*) # if no a number cd $( pwd | sed -r "s|(.*/$1[^/]*/).*|\1|" ) # search dir_name in current path, if found - cd to it ;; # if not found - not cd *) cd $(printf "%0.0s../" $(seq 1 $1)); # cd ../../../../ (N dirs) ;; esac >alias 'cd..'='cd_up' # can not name function 'cd..' 
$ cd /home/user/documents/projects/reports/2014-10-01 $ cd.. doc $ pwd > /home/user/documents 

you can use pushd . to remember one directory and popd to go back to it.

If there is a command I use a lot I will just make an alias.

Then you can just use .. to go one level up and . to go two levels up.

Cd — Go up several directories in linux, This nifty function supports going up in both directions. If you are in /a/b/c/d then ‘up 1’ will take you to /a/b/c, and so on. Thats pretty standard and covered in most of the other answers. Now, for the special part; using negative numbers takes you up from the other direction. So, if you are in /a/b/c/d, ‘up -1’ … Code sample# cd up to n dirs# using: cd.. 10 cd.. dirfunction cd_up()

Cd Up One Level Linux

We often use directories when working on a computer system. The directory is nothing but a folder or a location where a file is stored. We store our files in different directories. And sometimes we may need to change the folders. Instead of going to the folder directly, we can do this using cd (Change Directory) command.

On Linux and other Unix-like operating systems, the cd command can change the current working directory. Working in the current directory of a computer is also referred to as the user’s current directory. In this article, we will dive deeper into the cd command and give you a brief on cd up one level Linux.

cd Up One Level Linux

You are always in a home directory when you open the terminal. To change the directory and move to another directory cd command is used in the following ways.

Root directory navigation: Type “cd/” to enter the root directory.

Home directory navigation: we use the “cd~” or “cd” command to move into the home directory.

Up to one level: moving up one level in the directory, we use the “cd..” command in the following way:

Back one level: we use the “cd-” command to return back to the previous folder.

Multiple level navigation: we can use a complete path to go to the specific directory along with the cd command in the following way:

Читайте также:  Команда сохранить файл linux

Remember, there is space between cd and the specified path.

Directory name having spaces: if your directory name contains the spaces in the name, then the name should be written inside the quotes in the following way: e.g., “My home route” is my directory name the I will write as:

Conclusion

In this article, we have discussed the current working directory and cd command. Also, we have seen how to use the cd command in multiple navigations. We hope you enjoyed the article. Please visit our website to read more articles.

Cd Up One Level Linux, Instead of going to the folder directly, we can do this using cd (Change Directory) command. On Linux and other Unix-like operating systems, the cd command can change the current working directory. Working in the current directory of a computer is also referred to as the user’s current directory.

How to change the current directory in Go

Unlike Execute the ‘cd’ command for CMD in Go , I just want to really run cd directory_location using Go and change the current directory.

Say I am on ~/goproject, and I run, ./main in the terminal, I want to be at ~/goproject2 in the terminal.

cmd := exec.Command("bash", "-c", "cd", "~/goproject2") cmd.Run() 

But this didn’t actually change the current directory.

Usually if you need a command to run from a specific directory, you can specify that as the Dir property on the Command , for example:

cmd := exec.Command("myCommand", "arg1", "arg2") cmd.Dir = "/path/to/work/dir" cmd.Run() 

You want os.Chdir . This function will change the application working directory . If you need to change the shell working directory , your best bet is to look up how cd works and work back from that.

As you have discovered, you cannot use cd to change your current directory from inside an application, but with os.Chdir there is no need for it to work 🙂

home, _ := os.UserHomeDir() err := os.Chdir(filepath.Join(home, "goproject2")) if err != nil

How to change the current directory in Go, Usually if you need a command to run from a specific directory, you can specify that as the Dir property on the Command, for example: cmd := exec.Command («myCommand», «arg1», «arg2») cmd.Dir = «/path/to/work/dir» cmd.Run () You want os.Chdir. This function will change the application working …

How do I navigate up one directory from the terminal?

I can navigate down in directory using cd in the terminal. How do I navigate back up if I go too far?

cd .. will bring you back exactly one directory up.

You can string together those to go up multiple directories, e.g. up 3

Instead of typing cd .. multiple times, what you could to is to place the function bellow into your .bashrc somewhere at the top, save .bashrc , and run source .bashrc or just close and reopen a terminal. Now, you have a function that does cd.. exactly how many times you told it to.

$ cd /usr/share/backgrounds/ backgrounds:$ goUp 2 usr:$ 

Note that such method brings you back along the symlinks. Here’s what I mean:

$ namei "$PWD" f: /home/user/VirtualBox VMs/CentOS d / d home d user l VirtualBox VMs -> /mnt/ubuntu/vboxvms d / d mnt d ubuntu d vboxvms d CentOS $ goup 2 $ pwd /home/user 
See also
  • What are directories, if everything on Linux is a file?
  • Why is the current directory in the ls command identified as linked to itself?
Читайте также:  Проверка файловой системы linux btrfs

I found a simple way to go up.

../means one level up directory

you can use popd and pushd too, to «checkpoint» or «bookmark», or as I tend to describe it; «set a spawn-point»:

pushd ./ # set the spawn point to the current folder ./ 

go to another directory, like cd .. or whatever

popd # get back to where we set pushd 

This is, hopefully something useful for someone,

Have a great day reader!

How do I navigate up one directory from the terminal?, cd .. will bring you back exactly one directory up. You can string together those to go up multiple directories, e.g. up 3 cd ../../.. Instead of typing cd .. multiple times, what you could to is to place the function bellow into your .bashrc somewhere at the top, save .bashrc, and run source .bashrc or just close and …

Источник

cd Up One Level Linux

We often use directories when working on a computer system. The directory is nothing but a folder or a location where a file is stored. We store our files in different directories. And sometimes we may need to change the folders. Instead of going to the folder directly, we can do this using cd (Change Directory) command.

On Linux and other Unix-like operating systems, the cd command can change the current working directory. Working in the current directory of a computer is also referred to as the user’s current directory. In this article, we will dive deeper into the cd command and give you a brief on cd up one level Linux.

cd Up One Level Linux

You are always in a home directory when you open the terminal. To change the directory and move to another directory cd command is used in the following ways.

Root directory navigation: Type “cd/” to enter the root directory.

Home directory navigation: we use the “cd~” or “cd” command to move into the home directory.

Up to one level: moving up one level in the directory, we use the “cd..” command in the following way:

Back one level: we use the “cd-” command to return back to the previous folder.

Multiple level navigation: we can use a complete path to go to the specific directory along with the cd command in the following way:

Remember, there is space between cd and the specified path.

Directory name having spaces: if your directory name contains the spaces in the name, then the name should be written inside the quotes in the following way: e.g., “My home route” is my directory name the I will write as:

Conclusion

In this article, we have discussed the current working directory and cd command. Also, we have seen how to use the cd command in multiple navigations. We hope you enjoyed the article. Please visit our website to read more articles.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.

Источник

Оцените статью
Adblock
detector