0

I am new to Linux and am supposed to write a shell script to reorder the folders in a directory on the basis of ctime. The place where i am stuck is sorting the folders physically.

I have tried the following approaches:

  1. I was using combinations of ls sort command under an impression that it also changes the physical order but it is not affecting the order only listing the result.
  2. Using sort command alone hangs the terminal and then I am left with no other option other than canceling the command.

Kindly provide some reference for physical reordering of the folders. I have gone through articles but they were not much helpful.

What I am trying to achieve is to sort the folders in the directory on basis of last modified time. The main concern here is not how to extract time details of a folder in linux but what command can be helpful in sorting the folders physically on basis of last modified time.

Commands I have tried:

du -m --max-depth 1 | sort -rn
ls -1 | sort
ls -la | sort -k 1
  • 1
    Directories have no intrinsic order. The order will depend on the system locale. By "reordering", do you mean listing the directories and sorting them? Please edit your question and explain your task more clearly, and also show the scripts/commands you have already tried. – slhck Nov 13 '18 at 14:54
  • Please check . I have done the edits . – Nishant Singh Nov 13 '18 at 14:59
  • @slhck: They frequently do, and it depends on the filesystem. In most cases the order is not changeable by the user, so I hope the OP really meant "reorder the folder list" as in ls -ltc... – grawity Nov 13 '18 at 15:59
  • 1
    By "physical" do you mean "visual"? Are you trying to change the order the items are listed in a Nautilus window? Please edit the question to include the Linux distribution (Ubuntu, for instance). – Christopher Hostage Nov 13 '18 at 16:18
  • 1
    I am voting to close the question unless it is edited with the information given in the comments below. As it stands it's not answerable, as it is an XY problem. – slhck Nov 14 '18 at 18:50
0

If you want to list the contents of a directory by ctime, @grawity is correct, use

ls -ltc

If you want to list the contents of a directory based on the filesystem tree structure, you are wanting to sort by inode. You can do that using

ls -li | sort

If you want something else, please provide a sample input and a sample output.

Hope this helps

  • I have tried these. The thing is I want to sort the folders in the directory and not just display them . – Nishant Singh Nov 13 '18 at 17:30
  • 1
    What do you mean by "sort the folders in the directory and not just display them"? What are you looking to do once the folders in a given directory have been sorted? – Lewis M Nov 13 '18 at 17:32
  • 1
    The thing I am trying to achieve is sort the folders in a directory and then move the folders to another directory in a sorted order. Let me explain the scenario . We have a source folder where in we get documents per customer. For each customer we make a folder and then place his documents in those folder . Now we want to pick up folders in fcfs fashion i.e folder created first must be given priority . The approach that I have been asked to implement is first sort the folders in the source directory , then move these folders to a second directory with some additional conditions . – Nishant Singh Nov 13 '18 at 18:12
  • Good. Additional information. That is helpful. Could you provide sample input and sample output as well? That will give us an even better idea of what you are trying to accomplish. If you want to supply things like creation times, modified times, etc, please do so. The more details you give us, the better we will be able to help you. – Lewis M Nov 13 '18 at 18:19
  • 1
    @NishantSingh You should really edit your question and explain this – your original question really does not make a lot of sense without this information. Please also briefly read: xyproblem.info – slhck Nov 14 '18 at 9:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.